【问题标题】:FileMode and FileAccess and IOException: The process cannot access the file 'filename' because it is being used by another processFileMode 和 FileAccess 和 IOException:进程无法访问文件“文件名”,因为它正被另一个进程使用
【发布时间】:2013-08-22 23:28:31
【问题描述】:

我有一个应用程序 A,它生成一个用于跟踪的文本文件。
而应用程序 B 需要读取相同的文本文件并附加在邮件消息中。

但当应用程序 B 尝试读取文本文件时,出现以下错误:

IOException: 进程无法访问文件“文件名”,因为它 正在被另一个进程使用

有什么建议吗?也许更好地使用 FileMode 和 FileAccess?

应用A

if (File.Exists(nFile2)) File.Delete(nFile2);
                traceFile2 = File.Open(nFile2, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
                if (traceFile2 != null)
                {
                    var twt2 = new TextWriterTraceListener(traceFile2);

                    // http://www.helixoft.com/blog/archives/20
                    try
                    {
                        if (twt2.Writer is StreamWriter)
                        {
                            (twt2.Writer as StreamWriter).AutoFlush = true;
                        }
                    }
                    catch { }

                    var indiceTraceFile2 = Trace.Listeners.Add(twt2);
                    System.Diagnostics.Trace.WriteLine("INICIO: " + DateTime.Now.ToString());

应用程序 B

using (FileStream fileStream = File.Open(f, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                    {
                        var messageAttachment = new Attachment(fileStream, Path.GetFileName(f));
                        msgMail.Attachments.Add(messageAttachment);
                    }

【问题讨论】:

    标签: c# process ioexception


    【解决方案1】:

    您需要确保服务和阅读器都以非独占方式打开日志文件。注意 App A 的第 2 行和 App B 的第 1 行

    应用 A:

    if (File.Exists(nFile2)) 
        File.Delete(nFile2);
    traceFile2 =  new FileStream(nFile2, FileMode.Open, FileAccess.Write, FileShare.ReadWrite);
    if (traceFile2 != null)
    {
        var twt2 = new TextWriterTraceListener(traceFile2);
        // http://www.helixoft.com/blog/archives/20
        try
        {
            if (twt2.Writer is StreamWriter)
            {
                (twt2.Writer as StreamWriter).AutoFlush = true;
            }
        }
        catch { }
    
        var indiceTraceFile2 = Trace.Listeners.Add(twt2);
        System.Diagnostics.Trace.WriteLine("INICIO: " + DateTime.Now.ToString());
    

    和应用程序 B:

    using (FileStream fileStream = new FileStream(f, FileMode.Open, 
                                                     FileAccess.Read, 
                                                     FileShare.ReadWrite))
    {
        var messageAttachment = new Attachment(fileStream, Path.GetFileName(f));
        msgMail.Attachments.Add(messageAttachment);
    }
    

    【讨论】:

      【解决方案2】:

      当然,您可以同时读写同一个文件(通过不同的线程/进程)。

      这是一个示例代码。看看 FileStream 是如何创建的。

      string fname = "a.txt";
      
      //WRITER
      Task.Factory.StartNew(() =>
      {
          var f = new FileStream(fname, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite);
          var s = new StreamWriter(f);
          long l = 0;
          while (true)
          {
              s.WriteLine(l++);
              s.Flush();
              Task.Delay(1000).Wait();
          }
      });
      
      
      //READER
      Task.Factory.StartNew(() =>
      {
          Task.Delay(1000).Wait();
          var f = new FileStream(fname, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
          var s = new StreamReader(f);
      
          while (true)
          {
              var line = s.ReadLine();
              if (line == null) { Task.Delay(100).Wait(); continue; };
              Console.WriteLine("> " +  line + " <");
          }
      });
      

      【讨论】:

        【解决方案3】:

        您似乎没有使用 StreamWriter 类的 Dispose() 和 Close() 方法来释放文件。

        【讨论】:

        • 不释放应用程序A中的文件,因为应用程序A正在运行。两个应用程序同时运行。
        【解决方案4】:

        您需要从程序 A 中释放对文件的控制权。完成后尝试关闭或释放流写入器。

        或者您可以尝试using,如该问题的答案所述:Releasing access to files

        【讨论】:

        • 即使FileShare.ReadWrite?
        • 似乎其他一些答案使这一点无效。
        • 不释放应用程序A中的文件,因为应用程序A正在运行。两个应用程序同时运行。
        猜你喜欢
        • 1970-01-01
        • 2018-09-20
        • 2020-03-11
        • 1970-01-01
        • 2022-11-18
        • 1970-01-01
        • 2021-12-02
        • 2015-07-27
        • 1970-01-01
        相关资源
        最近更新 更多