How to open new OpenFileDialog automatically in Vista/Win7?
Asked Answered
P

3

0

I'm on Vista and I'm using Microsoft.Win32.OpenFileDialog class.

When I call ShowDialog() I get the old XP-style dialog: alt text

How do I get the new Vista-style dialog with fallback to the old one on WindowsXP? alt text

A bit of rumble:

I don't really understand why they didn't replace the dialog in vista, but kept both of them. Now legacy apps will never open new dialog, unless updated.

Preparation answered 7/10, 2010 at 19:37 Comment(4)
Does not it depends on your current system theme?Gambill
Which version of the .Net framework does the project target?Always
.NET 3.5 SP1 - should be finePreparation
What is your current system theme?Gambill
G
2

Yes, you'd have to upgrade to .NET 4.0 to get the new dialog. If you're stuck on 3.5 then you can use System.Windows.Forms.OpenFileDialog, it did get the update to use the new IFileDialog COM interface.

The fallback is automatic but you can use its AutoUpgradeEnabled property to force legacy, if necessary. Which it is not, unlikely that a .NET program would modify the dialog.

Gooseberry answered 7/10, 2010 at 19:52 Comment(5)
The code I put above works for me even compiled as 2.0 and compiled from VS2008. Are you sure it's the .NET framework version @Kugel? Can you try on your new project setting the .NET framework version down?Millicent
@Brian - that would be a miracle. Be sure to use the Microsoft.Win32 namespace, as used in a WPF program.Gooseberry
It works, but I'm reluctant to include Windows.Forms into WPF application.Preparation
@Hans: Not sure if it makes a diff but ya I was referencing a Winforms program.Millicent
@Preparation - it is not going to infect your WPF app with winforms manners. The class is a very thin wrapper around the native Windows interface, just a bunch of pinvoke you wouldn't want to maintain yourself. It is up to you.Gooseberry
M
1

The first dialog you showed is a save dialog not an open dialog.

You should only have to do this:

OpenFileDialog OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\";
openFileDialog1.Filter = "My files (*.myfile)|*.myfile|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
  //openFileDialog1.FileName
}
Millicent answered 7/10, 2010 at 19:41 Comment(4)
sorry, true, but this is question is valid for both open and save dialogs.Preparation
With the above code on a win7 machine with VS2008 I get the win7 dialog. Can you confirm it doesn't work for you?Millicent
hmm I opened new project a tried your code (modiefied return value in WPF) and the dialog is the new kind. hmm...Preparation
Maybe you have something referencing the old common control dll in your app config file or manifest file?Millicent
L
1

Reference System.Windows.Forms

using System.Windows.Forms

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    //Do Stuff
}
Latvina answered 7/10, 2010 at 19:46 Comment(5)
It appears I'm late to the party :( ... again.Latvina
"How do I get the new Vista-style dialog with fallback to the old one on WindowsXP?" It appears that is what's being asked. Not sure about the "fallback" but either way my answer works.Latvina
Your code is correct, I know. It opens new openfiledalog like @Brian R. Bondy's code. But, Kugel's question is about the appearance of openfiledialog.Gambill
actually this answer seems correct for my situation in .NET 3.5 I can get new-style by referencing windows.forms. But I don't think Sorax meant it this way.Preparation
That's why I was sure to include the reference to System.Windows.Forms. Win32 is the old version. I'm glad I could help. @Serkan, perhaps remove your downvote?Latvina

© 2022 - 2024 — McMap. All rights reserved.