I'm trying to send an attachment file using the code below, but the attachment dialog isn't opening. Instead, I'm getting an error message: "Input string was not in a correct format." Here's the code I'm using:
private void proto_Type_AI_Blackhead_God(object sender, RoutedEventArgs e)
{
try
{
OpenFileDialog attachment = new OpenFileDialog();
attachment.InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString();
attachment.Filter = "Image Files (*.jpg;*.bmp;*.gif)|*.jpg;*.bmp;*.gif|PDF Files|*.pdf|XML Files|*.xml";
if (attachment.ShowDialog() == DialogResult.OK)
{
filename = attachment.FileName;
filename = attachment.SafeFileName;
}
else
{
MessageBox.Show("No file selected.");
}
attachment = null;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Can anyone help me figure out why the dialog isn't working and why I'm seeing this error?
I'm surprised that you got this code compiled.
First of all, OpenFileDialog.ShowDialog()
returns bool?
, so have it properly checked (for HasValue
initally and then the value of Value
).
Then, why do you overwrite filename
variable? I assume filename
is some global variable here.
Further, having that fixed I had no problems running the code, filter string is perfectly correct semantically. Logically, jpegs, bmps and gifs are not XML files.