异常 - 文件已被其他程序使用

时间:2013-12-26 19:51:58

标签: c# file text io

我目前正在开发一个使用C#控制台应用程序编辑文本文档的项目。当我启动程序时,它要求用户在编辑现有文本文档或创建新文档之间进行选择。如果我创建一个新的,编辑它或其他什么,当我尝试使用“File.WriteAllLines(path,list.ToArray)”保存它时,我收到一个异常,说我无法访问该文件,因为它正被使用通过另一个程序。我曾尝试过一些东西,但似乎都没有用,所以这是我的代码:

    bool loop = false;
    string path = "";

    do
    {
        loop = false;

        Console.WriteLine("If you want to create a new text document type 1, or if you want to edit");
        Console.Write("an existing document type 2: ");
        string choice = Console.ReadLine();
        Console.WriteLine();
        Console.WriteLine();

        if (choice == "1")
        {
            Console.Write("Please enter the path, where you want your text document to be created: ");
            path = Console.ReadLine();
            Console.Write("Enter a name for your text document: ");
            path = (path + Console.ReadLine() + ".txt");

            File.Create(path);
            Console.WriteLine();
        }
        else if (choice == "2")
        {
            Console.Write("Text document path: ");
            path = Console.ReadLine();        
            Console.WriteLine();
        }
        else
        {
            loop = true;
            Console.WriteLine();
            MessageBox.Show("You can only type 1 or 2. Type '1' for creating a new text document and '2' for editting and existing document.", "Warning", MessageBoxButtons.OK);
        }



        if (loop == false)
        {
            bool repeat = true;

            do
            {
                string[] lines;

                try
                {
                    lines = File.ReadAllLines(path);
                }
                catch (Exception)
                {
                    lines = new string[1] { "EMPTY" };
                }

                List<string> LineList = new List<string>();
                int i = 1;

                foreach (var eachLine in lines)
                {
                    LineList.Add(eachLine);
                    Console.WriteLine("{0}. {1}", i, eachLine);
                    i++;
                }
                Console.WriteLine();

                Console.Write("Line to edit: ");
                int n = int.Parse(Console.ReadLine());
                n--;

                Console.Write("{0}. ", n + 1);
                try
                {
                    LineList[n] = LineList[n].Replace(LineList[n], Console.ReadLine());
                }
                catch (Exception)
                {
                    LineList.Insert(n, Console.ReadLine());
                }

                File.WriteAllLines(path, LineList.ToArray());  // Cannot access the file because it is being used by another program exception


                Console.WriteLine();

                Console.WriteLine("Would you like to continue editting the document? Y/N");
                string LoopEnd = Console.ReadLine();
                if (LoopEnd == "n" || LoopEnd == "N" || LoopEnd == "no" || LoopEnd == "NO" || LoopEnd == "No")
                {
                    repeat = false;
                }
                Console.WriteLine();

            } while (repeat);
        }
        Console.WriteLine();


    } while (loop);

谢谢!

2 个答案:

答案 0 :(得分:3)

创建文件时,您实际上是打开,就像在计算机上打开常规文件时一样。

使用using自动处理和关闭它:

using (File.Create(path))
{
    ///.....
}

答案 1 :(得分:1)

问题是您已打开文件但未关闭它。

您应该使用File.Create中的流然后将其关闭。

但我建议使用StreamWriter。

FileStream newFile = File.Create(path);
newFile.Close();

// ...

File.WriteAllLines(LineList.ToArray());

祝你好运。