Tuesday, January 15, 2013

WPF FolderBrowserDialog and DialogResult.OK error

Have you ever tried to display the FolderBrowserDialog in a WPF application to browse for a folder? And have you ever looked at the official examples and wondered why you got an error saying something with Nullable and that DialogResult.OK does not exist?

DialogResult result = fb.ShowDialog();

if (result == DialogResult.OK)
{
 String folderName = fb.SelectedPath;
}
It's because there are two DialogResult classes. There is System.Windows.Forms.DialogResult:
http://msdn.microsoft.com/en-us/library/system.windows.forms.dialogresult.aspx
And there is System.Windows.Window.DialogResult:
http://msdn.microsoft.com/de-de/library/system.windows.window.dialogresult.aspx
In our WPF application, it tries to use the latter, which leads to the error. The solution is, use the full path, like so:
DialogResult result = fb.ShowDialog();

if (result == System.Windows.Forms.DialogResult.OK)
{
 String folderName = fb.SelectedPath;
}
This works!

1 comment: