我正在 Visual Studio Ultimate 2012 上编写一个基于 Windows 窗体的应用程序。我放入了一个 opendialog 以打开和读取文件。这是代码: -
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
但是每次我尝试运行它时,当我从浏览器窗口中选择一个文件时,它会显示一个 filenotfound 异常,因为它在默认目录中查找文件(位于 Documents/VisualStudio/Projects/WindowsFormApplication/bin/debug/ openFileDialog1),不在我指定的目录中。请帮忙!!