How can I prevent an exception when I cancel an openfiledialog?
Solution 1
DialogResult result = fileDialog.ShowDialog();
if (result == DialogResult.OK) {
//returns a string for the directory
return fDialog.FileName;
}
return null; //not sure what you will return if they cancel
also, FileName is already a string, so no need to use .ToString()
on it
EDIT: fixed indenting
Solution 2
Check the dialog result and act accordingly:
private string ChoosePicture()
{
fDialog.Title = "Select Picture";
fDialog.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp; *.gif*;*.jpg";
fDialog.InitialDirectory = "C:";
DialogResult res = fDialog.ShowDialog();
if(res == DialogResult.OK)
{
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
//returns a string for the directory
return fDialog.FileName.ToString();
}
else
{
return null; // or something
}
}
Solution 3
Test to see if a file was selected:
fDialog.ShowDialog();
if (!string.IsNullOrEmpty(fDialog.FileName))
{
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
//returns a string for the directory
return fDialog.FileName.ToString();
} else {
return String.Empty;
}
Solution 4
DialogResult dresult=fDialog.ShowDialog();
Check if dresult==DialogResult.Ok
and only after proceed with file operations.
Solution 5
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
DialogResult res = fDialog.ShowDialog();
if (res == DialogResult.OK)
{
//returns a string for the directory
return fDialog.FileName.ToString();
}
return null;
Now it will work !
We should add properties to the dialogbox before its actually been shown. So when it opens, it will have all these properties when you open them for the first time.
Edit :okay you have added to the designer by the toolbox already and its by default all of these options. but if some add from code. it should be always before its being shown. I will leave this here. so that someone who does this
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
in code, will know that they should do these property addition before showing the dialog. Again, these true values are by default so unless u have mentioned false before elsewhere and making it true here.
Fuzz Evans
Updated on July 29, 2022Comments
-
Fuzz Evans almost 2 years
My program has a button which when clicked opens an openfiledialog to choose a picture:
private string ChoosePicture() { fDialog.Title = "Select Picture"; fDialog.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp; *.gif*;*.jpg"; fDialog.InitialDirectory = "C:"; fDialog.ShowDialog(); fDialog.AddExtension = true; fDialog.CheckFileExists = true; fDialog.CheckPathExists = true; //returns a string for the directory return fDialog.FileName.ToString(); }
Using a check on the dialogresult box hasn't resolved my issue either:
fDialog.AddExtension = true; fDialog.CheckFileExists = true; fDialog.CheckPathExists = true; DialogResult res = fDialog.ShowDialog(); if (res == DialogResult.OK) { //returns a string for the directory return fDialog.FileName.ToString(); } return null;
The code works if I do choose a picture and complete the file selection. However if I cancel the process at any point in between I get the exception "The path is not of a legal form". I am not sure which part I imagine I could take care of this with a
try-catch
, however I'm not positive which part is causing the issue? If I put atry catch
around the call to theChoosePicture()
method, I can at least stop it from crashing the program but the exception is still being thrown when no picture is selected in the fdialogbox. -
Fuzz Evans over 12 yearsI tried your result, still throws the error : "The path is not of a legal form"
-
Fuzz Evans over 12 yearsI tried your result, still throws the error : "The path is not of a legal form"
-
Anthony Shaw over 12 yearsat which line are you getting the error. If you cancel out of the dialog, it should never drop into the block where it does anything with the FileName property. Set a break point on the
if
statement and let us know what the value of result is when you click Cancel -
Anthony Shaw over 12 yearsI just took your original code and tried it, I do not get the error even when trying to get the filename, it is an empty string. Is your error happening after you return the value from your ChoosePicture() method?
-
Fuzz Evans over 12 yearsIt occurs somewhere else where I was basing other values off of the returned value. Your code is good, I should have looked at a larger scope. Thank you.
-
Fuzz Evans over 12 yearsIt occurs somewhere else where I was basing other values off of the returned value. Your code is good, I should have looked at a larger scope. Thank you.
-
King over 12 yearskeep the properties of the file dialog before showing the dialog. this way of adding properties to the control doesn't make any sense when it happens for the first time
-
Anthony Shaw over 12 years@King, fixed my answer. It was simply a copy and paste of the OP's code from the ShowDialog down. It wasn't causing any portion of his issue
-
King over 12 yearsHe has added from toolbox as does most of us but if he should add it by himself in code, those become very essential.