文件描述错误的 Openfiledialog 问题

Openfiledialog problem with File Description Error

我正在使用 VS2015 - Windows 表单。当我单击浏览按钮时,OpenFileDialog 运行良好。但是假设一旦我在刷新表单数据后重新单击按钮,OpenFileDialog 就会挂断。

我的问题看不懂。。有哪位高人可以指导一下吗?

MyFileNameStr = String.Empty;
openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\";
openFileDialog1.Filter = "(*.xlsx)|*.xls| All files (*.*)|*.*";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.Title = "Select Your Attachment File :- ";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFileDialog1.FileName.Length>0) {
    String MyDrawingFile = Path.GetFileName(openFileDialog1.FileName);
    myDataGrid1.CurrentRow.Cells["MyExcel_file"].Value = Path.GetFileName(openFileDialog1.FileName);
    MyFileNameStr = openFileDialog1.SafeFileName.ToString();
    MyFileNameStrs = openFileDialog1.SafeFileName.ToString().Split('_');
}

再次感谢

这是由于 如果您单击按钮并浏览文件,则该过程是 运行 在您的 excel 文件上。 如果您再次单击按钮,进程正忙于处理您的 excel 文件,应用程序将挂起。

我在下面添加了一些文件描述代码,效果很好。

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "D:\";
openFileDialog1.Title = "Select Your Attachment File :- ";

openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.Filter = "exe files | *.exe|All files (*.*)|*.*";

openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;

openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;

if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
MyDrawingFile = System.IO.Path.GetFileName(openFileDialog1.FileName).ToString();
MyFileNameStr = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName).ToString();
}

谢谢