-1

My C# application works perfectly unless trying to access the current date's log file because it is being used by another process but the file can be open through notepad. I asked this question a couple of weeks ago but did not receive an answer that resolved my issue.

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

GOAL: I want to read today's log file but it is currently in use. After reading the file, I am extracting certain strings into a new text file. But the file I want to read is being used by another process

SlopTonio
  • 1,105
  • 1
  • 16
  • 39

1 Answers1

0

Before Write/Read/Delete call below function to know file status

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

I am calling above function when i want to delete a file

/// 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(); 
           }
        }
 }
Rachit Patel
  • 854
  • 5
  • 12
  • 1
    The problem here is that in the timeframe the file could be locked even if at the time of execution it was not locked. – Ahmed ilyas May 26 '14 at 11:22