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

Today is: Friday, 21 November, 2008
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 : Friday, 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

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