How to import and use native Open file dialog? Does I have any benefits of it in application that not use Windows Forms nor WPF? Or just reference it?
-
If not WinForms or WPF, what's the GUI foundation for the app? Or are you trying to open a dialog from a console application? – Dan J Jun 17 '11 at 19:37
-
mOGRE (3D app). I only want to avoid unneeded dependencies and loads. – newGuest Jun 17 '11 at 20:11
3 Answers
The standard file dialogs offered by WinForms and WPF are merely wrappers around the native dialogs. So it makes most sense to use these managed C# classes.
If you wish to customise the dialogs in any way (e.g. adding controls to the dialog) then there may be a case for using the native API. But that's mainly because you need to use a different customisation technique for XP from that used for Vista/7. Even in pure native code, customising file dialogs is non-trivial and you should really avoid doing it if you reasonably can.

- 601,492
- 42
- 1,072
- 1,490
-
2This, although I think WPF < 4 *might* have been showing the wrong kinds of open/save dialogs which raised the question. I know WPF 4 displays the correct ones. – BoltClock Jun 17 '11 at 19:38
-
@BoltClock You mean WPF<4 shows the legacy dialogs on Vista/7? – David Heffernan Jun 17 '11 at 19:43
-
Apparently so. There are reports and [screenshots](http://www.thomasclaudiushuber.com/blog/2008/04/12/vistas-savefiledialog-and-openfiledialog-in-wpf/) around from WPF's early years; the workaround was to use the classes in the WinForms namespace instead of `Microsoft.Win32`. – BoltClock Jun 17 '11 at 19:45
There are wrappers for the common dialogs in the Windows API Code Pack. Some versions of Windows Forms and WPF, released before Vista, used some parameters when constructing the dialogs that caused Vista (or Windows 7) to say "oh, you know what you want, I won't give you the all-singing all-dancing new versions of the dialogs." Since then there have been service packs, new releases etc that took care of that. If for some reason you can't get the right dialogs, start by installing any service packs for whatever version of Visual Studio you're using. If you still get no satisfaction, try the Code Pack.

- 18,808
- 8
- 56
- 85
-
+1 good advice, get up to date with the managed wrappers if the problem is the WPF dialog showing the legacy file dialog on Vista/7 – David Heffernan Jun 17 '11 at 19:47
This is a matter of what you're doing with your application. If it's written using Windows Forms, use System.Windows.Forms.OpenFileDialog; if it's written using WPF see here: OpenDialog for WPF if for some peculiar reason you must go against the Win32 api, go ahead and read up on GetOpenFileName here: http://msdn.microsoft.com/en-us/library/ms646927%28v=vs.85%29.aspx