取消 openfiledialog 时如何防止异常?
我的程序有一个按钮,单击该按钮会打开一个打开文件对话框来选择图片:
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();
}
使用对话框结果框上的检查也没有解决我的问题:
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;
如果我选择图片并完成文件选择,则代码可以工作。但是,如果我在中间的任何时候取消该过程,我会得到异常“路径不是合法形式”。我不确定我认为哪一部分可以通过 try-catch 来解决这个问题,但是我不确定是哪一部分导致了问题?如果我在对 ChoosePicture()
方法的调用周围放置一个 try catch
,我至少可以阻止它使程序崩溃,但在没有图片时仍然会抛出异常在 f 对话框中选择。
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 a try catch
around the call to the ChoosePicture()
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
另外,FileName 已经是一个字符串,因此无需在其上使用
.ToString()
编辑:修复缩进
also, FileName is already a string, so no need to use
.ToString()
on itEDIT: fixed indenting
检查对话框结果并采取相应措施:
Check the dialog result and act accordingly:
测试是否选择了文件:
Test to see if a file was selected:
DialogResult dresult=fDialog.ShowDialog();
检查是否
dresult==DialogResult.Ok
并且仅在继续文件操作后。DialogResult dresult=fDialog.ShowDialog();
Check if
dresult==DialogResult.Ok
and only after proceed with file operations.现在可以了!
我们应该在对话框实际显示之前向其添加属性。所以当它打开时,它会在你第一次打开它们时具有所有这些属性。
编辑:好的,您已经通过工具箱添加到设计器中,并且默认情况下包含所有这些选项。但如果有些是从代码中添加的。它应该总是在显示之前。我会把这个留在这里。这样,在代码中执行此操作
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
的人就会知道他们应该在显示对话框之前添加这些属性。同样,这些 true 值是默认的,所以除非您之前在其他地方提到过 false 并在此处将其设置为 true。
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.
您可以这样做,而不是
return fDialog.FileName;
和DialogResult.Cancel
是一个更好的选择,因为您正在寻找取消而不是确定结果。You can just do it like this instead of
return fDialog.FileName;
andDialogResult.Cancel
is a better option since your looking for a cancel and not for the OK result.我添加了一个布尔值并检查是否选择了文件
我防止了这样的错误。
i added a boolean and check if file selected or not
i prevent this error like that.