C # and WPF, ShowFIleDialog throwing excrement
I have a problem with my code. I am using OpenFileDialog in my project and when I call ShowDialog method an exception is thrown. I do not understand why.
private void open_FileMenu(object sender, RoutedEventArgs e)
{
OpenFileDialog browser = new OpenFileDialog();
browser.AddExtension = true;
browser.Filter = "Audio, Video File | *.wma; *.mp3; *.wmv";
browser.Title = "Choose your file";
if (browser.ShowDialog() == System.Windows.Forms.DialogResult.Yes) // Exception thrown here
{
try
{
string FileName = browser.FileName;
MyMedia.Source = new Uri(FileName);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
This exception is specified
A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in WindowsBase.dl
More information: Invalid parameter
Can anyone help me?
+3
source to share
2 answers
System.Windows.Forms.dllCommonDialog.ShowDialog()
comes in to WinForms and returns .DialogResult
In WPF CommonDialog.ShowDialog()
comes from PresentationFramework.dll and returnsbool?
This naturally leads to a lot of confusion. Ultimately you want this instead.
if (browser.ShowDialog() == true)
+4
source to share
This works for me:
OpenFileDialog browser = new OpenFileDialog();
browser.AddExtension = true;
browser.Filter = "Audio, Video File | *.wma; *.mp3; *.wmv";
browser.Title = "Choose your file";
string FileName;
bool? res = browser.ShowDialog(); // No exception thrown here
if (res ?? false)
{
try
{
FileName = browser.FileName;
//MyMedia.Source = new Uri(FileName);
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
+1
source to share