3

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.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
Xenira
  • 81
  • 7
  • have you used the debugger..? also look up some of the attributes for File.IO class when reading writing sharing etc.. – MethodMan Jun 09 '15 at 20:37
  • @MethodMan The code works fine with files, that are not opened in another aplication if it is that what you ment. – Xenira Jun 09 '15 at 20:44

0 Answers0