C# IOException:进程无法访问文件';文件路径';因为它正被C中控制台应用程序中的另一个进程使用#

C# IOException:进程无法访问文件';文件路径';因为它正被C中控制台应用程序中的另一个进程使用#,c#,ioexception,streamwriter,error-log,C#,Ioexception,Streamwriter,Error Log,我正在运行Console_应用程序-A,在其中调用另一个Console_应用程序-B(在其中创建错误/异常日志文件) 但当我单独运行Console_应用程序-B时,它工作正常,但当我运行Console_应用程序-A时,当应用程序需要在日志文件中写入错误时,我会遇到一个异常。(Error.txt) IOException:进程无法访问文件“Error.txt”,因为它 正在被另一个进程使用 请在这个问题上指导我 用于写入错误日志的代码 public static bool IsFileLocked

我正在运行Console_应用程序-A,在其中调用另一个Console_应用程序-B(在其中创建错误/异常日志文件)

但当我单独运行Console_应用程序-B时,它工作正常,但当我运行Console_应用程序-A时,当应用程序需要在日志文件中写入错误时,我会遇到一个异常。(Error.txt)

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

请在这个问题上指导我

用于写入错误日志的代码

public static bool IsFileLocked(FileInfo file)
{
 FileStream stream = null;
 try
 {
 stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None);
 }
 catch (IOException)
 {
  return true;
 }
 finally
 {
  if (stream != null)
  stream.Close();
  }
  return false;
 }

catch(异常e)
{
字符串filePath=Path.GetDirectoryName(System.Reflection.Assembly.getExecutionGassembly().Location)+“\\Error.txt”;
FileInfo FInfo=新的FileInfo(filePath);
var FileState=IsFileLocked(FInfo);
while(文件状态){
FileState=IsFileLocked(FInfo);
}
如果(!FileState){
使用(StreamWriter writer=newstreamwriter(filePath,true))
{
writer.WriteLine(“Message:+e.Message+”
“+Environment.NewLine+”StackTrace:+e.StackTrace+”+Environment.NewLine+”日期:“+DateTime.Now.ToString()); writer.WriteLine(Environment.NewLine+“-----------------------------------------------------------------------------------------”+Environment.NewLine); writer.Dispose(); } } }
无需先检查文件是否已锁定,然后再访问它,因为在检查和访问之间,其他一些进程可能仍会锁定文件

using System;
using System.IO;

class DirAppend
{
    public static void Main()
    {
        using (StreamWriter w = File.AppendText("log.txt"))
        {
            Log("Test1", w);
            Log("Test2", w);
        }

        using (StreamReader r = File.OpenText("log.txt"))
        {
            DumpLog(r);
        }
    }

    public static void Log(string logMessage, TextWriter w)
    {
        w.Write("\r\nLog Entry : ");
        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
        w.WriteLine("  :");
        w.WriteLine("  :{0}", logMessage);
        w.WriteLine ("-------------------------------");
    }

    public static void DumpLog(StreamReader r)
    {
        string line;
        while ((line = r.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }
}

Source-

两个应用程序同时访问同一文件?请添加代码以写入日志文件。不,两个应用程序使用的文件不同。错误消息很明显,您试图访问一个文件,但由于被另一个进程使用,因此无法访问。您还没有粘贴代码,但我猜实现IDisposable并将代码包装到using语句中会起作用。希望能帮我把添加的代码也写进日志文件,请检查一下并帮我解决!
using System;
using System.IO;

class DirAppend
{
    public static void Main()
    {
        using (StreamWriter w = File.AppendText("log.txt"))
        {
            Log("Test1", w);
            Log("Test2", w);
        }

        using (StreamReader r = File.OpenText("log.txt"))
        {
            DumpLog(r);
        }
    }

    public static void Log(string logMessage, TextWriter w)
    {
        w.Write("\r\nLog Entry : ");
        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
        w.WriteLine("  :");
        w.WriteLine("  :{0}", logMessage);
        w.WriteLine ("-------------------------------");
    }

    public static void DumpLog(StreamReader r)
    {
        string line;
        while ((line = r.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }
}