【问题标题】:Exception - file is already being used by another program异常 - 文件已被另一个程序使用
【发布时间】:2013-12-26 19:51:57
【问题描述】:

我目前正在开发一个使用 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);

谢谢!

【问题讨论】:

  • 文件打开了吗? (记事本什么的..)
  • 你打算什么时候关闭File.Create(path);创建的文件?看看它返回什么...
  • 它是一个 .txt 文件。关闭文件是什么意思? File.Create 只是创建文件不是吗?

标签: c# file text io


【解决方案1】:

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

使用using自动处理和关闭:

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

【讨论】:

  • @aj.toulan 谢谢!出于多种原因,这确实是一种很好的做法。
【解决方案2】:

问题是你打开了文件并没有关闭它。

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

但我推荐使用 StreamWriter。

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

// ...

File.WriteAllLines(LineList.ToArray());

祝你任务顺利。

【讨论】:

  • 是的,它奏效了。谢谢您的帮助。我对 C# 有点陌生,这是我第一次听说“流”这个东西。
猜你喜欢
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
  • 2018-01-30
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
相关资源
最近更新 更多