C#, WPF - OpenFileDialog does not appear
There are a large number of possible failure modes for OpenFileDialog. Using one exposes your app to just about any shell extension that's installed on your machine. Many of which can be very destabilizing, it isn't that likely that the extension author has checked if it works properly in a WPF process.
Tackle this problem by running SysInternals' AutoRuns utility. Click the Explorer tab and look for the groups that have "ShellEx" in their name. Uncheck anything that wasn't published by Microsoft. Reboot and check if the problem is solved.
This happened to me recently. The problem was the Main
method wasn't marked as an STAThread
which will cause the WPF OpenFileDialog's ShowDialog
method to block indefinitely.
static void Main(string[] args){ var openFileDialog = new OpenFileDialog(); var result = openFileDialog.ShowDialog();}
will never exit or throw an exception, whereas
[STAThread] static void Main(string[] args){ var openFileDialog = new OpenFileDialog(); var result = openFileDialog.ShowDialog();}
will work as expected.
I am experiencing a similar problem, and as Garrett suggested, it is an STA issue. I have been struggling with STA issues a lot in the past few months, as I need to launch screens from a console window (Testing purposes) - this means that the calling thread is not STA, but can be simulated in something like the following:
[STAThread] private void Execute() { try { Thread t = new Thread(() => { OpenFileDialog dlg = new OpenFileDialog(); // The following would not return the dialog if the current // thread is not STA var result = dlg.ShowDialog(); }); t.SetApartmentState(ApartmentState.STA); t.Start(); } catch (Exception ex) { // Handle the exception ex.LogException(); } }
Unforunately, it did not work for me to just mark the method as STAThread, I had to launch the operation in a thread marked as STA.