只有在调试器外部运行时才会发生FileNotFoundException

时间:2013-05-09 14:52:01

标签: c# visual-studio-2010

我有这个简单的代码打开一个txt文件:

  

StreamReader sourceFile = File.OpenText(fileName)

事情是,当我按ctrl -f5启动程序时,我得到的文件不存在错误。 但是当我按f11一步一步走的时候,一切都顺利进行,没有错误,或者发生了什么,我得到了理想的结果。 任何想法可能是什么原因?

我正在使用Visual Studio C#express 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);
            }
        }

1 个答案:

答案 0 :(得分:2)

这是运行路径的问题:调试,你在bin \ debug文件夹(文件所在的位置)启动程序,同时使用ctrl + F5运行而不调试,程序在bin \ release文件夹上启动。