Link to home
Start Free TrialLog in
Avatar of dvplayltd
dvplayltdFlag for Bulgaria

asked on

C#” How to read text file without locking it, so other application could write in it.

Dear experts

I wrote a Windows Form Application which should read a log text file, while it is being written from other application ¿ This other application in every 10-100 sec. add a row to this text file. I have no idea what access mode this other application use with the file, but I guess it open the file with add mode, wrote new lines info and then close the file open pointer. It is on C++ or Deplhi

 So I’m wondering what is my best tactics? I guess it is better I to read all the file at once, to close it and then to start cycle between its line and add my logic.

 Or it will be better first to copy the file on other direction on HDD and then to read this copied file?

 The text file I should read is with 200-2000 lines, not so good as every day it create a new one.

Second question – I plan to use StreamReader , this is the right choice? I need .NET library which read the text files without being locked it at all. I wrote in my DB which is the last row I have been read, so my only problem is to NOT block the other application access.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
using (var fileStream = new FileStream("foo.bar", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
using (var textReader = new StreamReader(fileStream))
{
    var content = textReader.ReadToEnd();
}

http://stackoverflow.com/questions/3448230/how-can-i-read-a-text-file-without-locking-it
Have you actually tried to read it?  If it is failing maybe it is the other app attempts to use an exclusive access to the file - which means the other app is coded so what you want would never work.
Avatar of dvplayltd

ASKER

10x