forge

Solving “The process cannot access the file because it is being used by another process” in .NET

While working on a log parser (for SharePoint ULS logs ) I ran into a problem in trying to open the current file.  I got the “The process cannot access the file because it is beung used by another process” exception back.  After playing with the way the file was opened I was able to come up with a technique that would allow me to open the file and read from it.

Here’s the line of code to use:

Help Your SharePoint User

FileStream fs = new FileStream(file.FullName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

This will open the file even if the program is still logging to it.  It works great if you want to read whatever’s already written to the file.