Reading and writing a file that is being used by another program

I am trying to access a file using the following code:

if (File.Exists(path))
{
    var lines = File.ReadLines(path);
    foreach (string s in lines) 
    {
        Match m = Regex.Match(s, findComment);
        if (m.Success) 
        {
            result.Enqueue(new Command(m.Groups[1].Value, m.Groups[2].Value));
        }
    }

    File.WriteAllText(path, String.Empty);
}

      

The file I want to access is a log file from another application, and whenever I try to access the file, it throws an exception. I tried to edit the file using notepad ++ and it worked and now I am wondering why notepad ++ can write to this file while my code cannot.

I found several solutions for writing only, but nothing to write locked file.

+3


source to share





All Articles