我打开一个txt文件,这个简单的代码外运行时出现:FileNotFoundException异常仅调试
的StreamReader的SourceFile = File.OpenText(文件名)
的事情是,当我按ctrl-f5启动程序,我得到一个文件不存在的错误。 但当我按f11一步一步走,一切运行平稳,没有错误或发生了什么,我得到了预期的结果。 任何想法可能是什么原因呢?
我使用Visual Studio的C#2010年快递
的代码在Program.cs中:
Class1.ReadPointsFile(@ “Points.txt”);
功能:
public void ReadPointsFile(string fileName)
{
if (!File.Exists(fileName))
{
Console.WriteLine("File doesn't exist.");
return;
}
using (StreamReader sourceFile = File.OpenText(fileName))
{
string inputLine;
int arraySize;
arraySize = Convert.ToInt32(sourceFile.ReadLine());
pointsArray = new Point2D[arraySize];
int i_keepTrack = 0;
inputLine = sourceFile.ReadLine();
do
{
string[] Coordinations = inputLine.Split(' ');
pointsArray[i_keepTrack] = new Point2D(double.Parse(Coordinations[0]), double.Parse(Coordinations[1]));
i_keepTrack++;
inputLine = sourceFile.ReadLine();
} while (inputLine != null);
}
}
您不是在早期版本中创建文件,是吗? – 2013-05-09 14:53:16
路径是相对还是绝对? – 2013-05-09 14:53:19
txt文件位于项目目录/ bin/debug @Josh我没有在这个项目中创建任何文件,我只是从文件中读取。 – Elia 2013-05-09 14:56:49