OpenFileDialog 在显示浏览对话框之前抛出 System.IO.FileNotFoundException
OpenFileDialog throwing System.IO.FileNotFoundException before it shows the browse dialog
我开始在 .NET 5.0 中创建一个简单的 OpenFileDialog,
但我得到一个
System.IO.FileNotFoundException: 'Unable to find the specified file.'
在我什至可以看到浏览对话框之前出现异常。
当我单击 m_FileUploader
按钮时,对话框应该会打开。这是我使用的代码:
Main.Designer.cs
//
// m_FileUploader
//
this.m_FileUploader.Location = new System.Drawing.Point(508, 17);
this.m_FileUploader.Name = "m_FileUploader";
this.m_FileUploader.Size = new System.Drawing.Size(234, 52);
this.m_FileUploader.TabIndex = 0;
this.m_FileUploader.Text = "Browse descr_names.txt";
this.m_FileUploader.UseVisualStyleBackColor = true;
this.m_FileUploader.Click += new System.EventHandler(this.FileUploader_Click);
//
// m_OpenFileDialog
//
this.m_OpenFileDialog.Filter = "Names |*descr_names.txt";
this.m_OpenFileDialog.InitialDirectory = "Documents";
this.m_OpenFileDialog.Title = "Open descr_names.txt";
this.m_OpenFileDialog.RestoreDirectory = true;
Main.cs
private void FileUploader_Click (Object sender, EventArgs e) {
if (m_OpenFileDialog.ShowDialog() == DialogResult.OK) { // Exception throws here
// Do stuff with file
}
}
当我在调试中单击继续时,其余代码正确执行,但独立可执行文件不喜欢此异常并停止应用程序。
我在这里忘记了什么?
您应该为 InitialDirectory
使用不同的值,因为值 Documents
可能无法正确处理。相反,你应该这样做:
using System;
// Get path for User's documents folder from .NET
var pathToDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
this.m_OpenFileDialog.InitialDirectory = pathToDocs;
见documentation for more details about SpecialFolder
. Or InitialDirectory
documentation。
我开始在 .NET 5.0 中创建一个简单的 OpenFileDialog,
但我得到一个
System.IO.FileNotFoundException: 'Unable to find the specified file.'
在我什至可以看到浏览对话框之前出现异常。
当我单击 m_FileUploader
按钮时,对话框应该会打开。这是我使用的代码:
Main.Designer.cs
//
// m_FileUploader
//
this.m_FileUploader.Location = new System.Drawing.Point(508, 17);
this.m_FileUploader.Name = "m_FileUploader";
this.m_FileUploader.Size = new System.Drawing.Size(234, 52);
this.m_FileUploader.TabIndex = 0;
this.m_FileUploader.Text = "Browse descr_names.txt";
this.m_FileUploader.UseVisualStyleBackColor = true;
this.m_FileUploader.Click += new System.EventHandler(this.FileUploader_Click);
//
// m_OpenFileDialog
//
this.m_OpenFileDialog.Filter = "Names |*descr_names.txt";
this.m_OpenFileDialog.InitialDirectory = "Documents";
this.m_OpenFileDialog.Title = "Open descr_names.txt";
this.m_OpenFileDialog.RestoreDirectory = true;
Main.cs
private void FileUploader_Click (Object sender, EventArgs e) {
if (m_OpenFileDialog.ShowDialog() == DialogResult.OK) { // Exception throws here
// Do stuff with file
}
}
当我在调试中单击继续时,其余代码正确执行,但独立可执行文件不喜欢此异常并停止应用程序。
我在这里忘记了什么?
您应该为 InitialDirectory
使用不同的值,因为值 Documents
可能无法正确处理。相反,你应该这样做:
using System;
// Get path for User's documents folder from .NET
var pathToDocs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
this.m_OpenFileDialog.InitialDirectory = pathToDocs;
见documentation for more details about SpecialFolder
. Or InitialDirectory
documentation。