C# FileNotFoundException仅在调试器外部运行时发生

C# FileNotFoundException仅在调试器外部运行时发生,c#,visual-studio-2010,C#,Visual Studio 2010,我有一个打开txt文件的简单代码: StreamReader sourceFile=File.OpenText(文件名) 问题是,当我按ctrl-f5启动程序时,我得到一个文件不存在错误。 但是,当我按f11键一步一步走的时候,一切都很顺利,没有错误,或者发生过什么,我得到了想要的结果。 你知道这是什么原因吗 我正在使用visual studio C#express 2010 Program.cs中的代码: Class1.ReadPointsFile(@“Points.txt”) 职能: pub

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

StreamReader sourceFile=File.OpenText(文件名)

问题是,当我按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);
            }
        }

这是运行路径的一个问题:调试,您在bin\debug文件夹(文件所在的文件夹)上启动程序,同时使用ctrl+F5在不调试的情况下运行,程序在bin\release文件夹上启动。

您不是在前面的一行中创建文件,是吗?路径是相对的还是绝对的?txt文件位于项目目录/bin/debug@Josh中。我没有在此项目中创建任何文件,我只是从文件中读取。在项目属性的“调试”选项卡中,是否指定了“工作目录”?@Adam它被指定为filename.txt。我已经编辑了这篇文章,现在包含了代码。谢谢,这已经解决了:)不,你至少需要将构建配置切换到发布。只需按Ctrl+F5组合键并不会更改目标文件夹。