日志文件正在被其他进程使用

本文关键字:进程 其他 文件 日志 | 更新日期: 2023-09-27 18:05:04

我的c#应用程序工作完美,除非试图访问当前日期的日志文件,因为它正在被另一个进程使用,但该文件可以通过记事本打开。几个星期前我问过这个问题,但没有收到解决我问题的答案。

private static void WriteFile(string fileToRead, string fileToWrite, string mySearchString, xmlData counter)
{
   counter.hitcount = 0;
   Stream stream = File.Open(fileToRead, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
   {
        StreamReader sr = new StreamReader(stream);
       while (!sr.EndOfStream)
       {
          using (var sw = new StreamWriter(fileToWrite, true))
           {
               if (sw.BaseStream.Position == 0)
               {
                   //write header only once
                   sw.WriteLine("date time s-sitename s-ip cs-method cs-uri-stem cs-uri-query s-port cs-username c-ip cs(User-Agent) cs(Cookie) sc-status sc-substatus sc-win32-status sc-bytes time-taken");
               }
               var count = 1;
               while (sr.Peek() != -1)
               {
                   var line = sr.ReadLine();
                   // skips 4 lines (headers of log file)
                   if (count > 4)
                   {
                       if (line != null && line.Contains(mySearchString))
                       {
                           sw.WriteLine(line);
                           counter.hitcount++;
                       }
                   }
                   count++;
                     sr.Close();
                     sw.Close();
               }
           }
       }
   }
}

目的:我想读取今天的日志文件,但它目前正在使用。读取文件后,我将某些字符串提取到一个新的文本文件中。但是我要读取的文件正在被另一个进程使用

日志文件正在被其他进程使用

在写入/读取/删除之前调用下面的函数来了解文件状态

 /// <summary>
    /// This function is used to check specified file being used or not
    /// </summary>
    /// <param name="file">FileInfo of required file</param>
    /// <returns>If that specified file is being processed 
    /// or not found is return true</returns>
    public static Boolean IsFileLocked(FileInfo file)
    {
        FileStream stream = null;
        try
        {
            //Don't change FileAccess to ReadWrite, 
            //because if a file is in readOnly, it fails.
            stream = file.Open
            (
                FileMode.Open, 
                FileAccess.Read, 
                FileShare.None
            );
        }
        catch (IOException)
        {
            //the file is unavailable because it is:
            //still being written to
            //or being processed by another thread
            //or does not exist (has already been processed)
            return true;
        }
        finally
        {
            if (stream != null)
                stream.Close();
        }
        //file is not locked
        return false;
    }

当我想删除一个文件时,我正在调用上面的函数

/// This function is used to delete all files inside a folder 
 public static void CleanFiles()
 {
        if (Directory.Exists("FOLDER_PATH"))
        {
            var directory = new DirectoryInfo("FOLDER_PATH");
           foreach (FileInfo file in directory.GetFiles())
           { 
              if(!IsFileLocked(file)) file.Delete(); 
           }
        }
 }