【发布时间】:2019-12-28 11:32:38
【问题描述】:
我正在使用 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('_');
}
再次感谢
【问题讨论】:
-
对此有多种解释,这个问题并不能帮助我们弄清楚哪一种是正确的。 “文件描述错误”是什么意思?
-
你的main方法有没有[STAThread]属性?
-
您的表单上有 OpenFileDialog 组件吗?您引用的是
Microsoft.Win32吗?试试using (var ofd = new System.Windows.Forms.OpenFileDialog()) { ...rest of the code... }。您还应该提及从何处调用此代码。 -
,,,你不需要检查这个
&& openFileDialog1.FileName.Length>0。 -
带表单声明 OpenFileDialog OpenFileDialog1=new OpenFileDialog()
标签: c# winforms visual-studio-2015 openfiledialog