Read and Write file that is used by another program

100 views Asked by At

I am trying to access a file using the folowing 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 haved tryed to edit the file with notepad ++ and it worked and now I am wondering, why notepad ++ can write to that file and my code can not.

I have found some solutions for write only, but nothing to write a locked file.

0

There are 0 answers