You are not Logged in
Would you like to Login or Register

Today is: 03 September 2010
Check this months hot topics

Reading a file which is locked by another process

The following code snippet shows how to read a file which is locked by another process..

FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);

while (!logFileReader.EndOfStream)
{
    string line = logFileReader.ReadLine();
    // Your code here
}

// Clean up
logFileReader.Close();
logFileStream.Close();
kick it on DotNetKicks.com del.icio.us digg Mister Wong YahooMyWeb Reddit Furl Spurl blogmarks
Paul Hayman Skype
Author : Paul Hayman
Published : 23 June 2006

Paul is the COO of kwiboo ltd consultant and has more than a decade of IT consultancy experience. He has consulted for a number of blue chip companies and has been exposed to the folowing sectors: Utilities, Telecommunications, Insurance, Media, Investment Banking, Leisure, Legal, CRM, Pharmaceuticals, Interactive Gaming, Mobile Communications, Online Services. Paul is the COO and co-founder of kwiboo (http://www.kwiboo.com/) and is also the creator of GeekZilla.

Comments

Steve said:

This doesn't work, try a file locked by the OS or an SQL Server mdf file

December 04, 2007 - 9:04 AM

Karthik said:

It does not work. I have a file locked by OS as well.

December 17, 2007 - 4:33 PM

Rob said:

It worked for me. I'm reading Log Files generated by IIS.

I had the same code as in the example with the excepetion of FileShare mode. I used FileShare.Write but changed it now to FileShare.ReadWrite.

thanks..

Rob

http://www.psa.sentics.eu

January 26, 2008 - 10:30 PM

Ferret said:

Worked for me, MS documents. Thanks!

September 03, 2008 - 5:54 AM

Francesco said:

Thanks it works.

December 12, 2008 - 9:22 AM

Brad said:

The code worked great for me! Still do not understand why setting access to read did not while read/write did.........

Almost resorted to copyinng the file prior to reading and a plan'C'-bringing in some scripting.

Thanks for your help

-BM

April 17, 2009 - 3:10 AM

Sigi said:

Thanks a lot. Works great for me. My log file is locked by a pipe process.

December 08, 2009 - 9:25 AM

Hans said:

Worked for me too. Reading locked IIS logfiles..

Thanks!

February 05, 2010 - 7:41 PM

Corey said:

Thanks Paul. This worked great for a log file locked by Log4Net DLL.

April 08, 2010 - 4:01 PM

Add Comment

Enter your comment below and it will be submitted for moderation.

Your Name

Add Tag

Please enter tags for this article, seperated by semi-colon ;

View Tag's by : # articles | # views