Solved

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

Posted on 2013-06-11
4
1,223 Views
Last Modified: 2013-06-11
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.
0
Comment
Question by:dvplayltd
4 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
Comment Utility
since u need the last lines written to the file u might want to check ReverseLineReader implementation here:
http://stackoverflow.com/questions/452902/how-to-read-a-text-file-reversely-with-iterator-in-c-sharp
0
 
LVL 15

Expert Comment

by:Minh Võ Công
Comment Utility
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
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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.
0
 

Author Closing Comment

by:dvplayltd
Comment Utility
10x
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now