另一个进程正在使用的日志文件

时间:2014-05-26 11:12:29

标签: c#

我的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();
               }
           }
       }
   }
}

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

1 个答案:

答案 0 :(得分:0)

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

 /// <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(); 
           }
        }
 }