How to open new OpenFileDialog automatically in Vista/Win7?

喜夏-厌秋 提交于 2019-12-12 01:43:00

问题


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

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

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

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.


回答1:


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.




回答2:


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
}



回答3:


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
}


来源:https://stackoverflow.com/questions/3885245/how-to-open-new-openfiledialog-automatically-in-vista-win7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!