Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Here is the case:

A text file created by a 3th party app. The other app is actively appending at that moment in time. It does so in batches every 1 min or so.
I can open the text file with notepad and see all the data in there.
If I try to modify it, notepad will not allow me to save it. Normal I would say.

Now I access the same file with the code below.
<br />
string line;<br />
sr = new StreamReader("TestFile.txt");<br />
line = sr.ReadLine();<br />
MessageBox.Show(line); <br />


I get no exceptions, but the messagebox only pops up after I close the 3th party app. :confused:
I seems that my app sort of stalls at the readline. How to get around this?
I want it to continue so I can read the whole file. Should be possible since notepad can.
Posted

1 solution

I guess, you are trying to opening the file while in use.

Try the below block. May be it help.

C#
FileStream fs = new FileStream("TestFile.txt", FileMode.Open, FileAccess.Read);
using (StreamReader reader = new StreamReader(fs))
{
    MessageBox.Show(reader.ReadLine());
}


Mark it as Answer if it is helpful.
 
Share this answer
 
v2
Comments
Vincent Beek 2-Mar-11 3:05am    
thx. I will be able to try your advise later today.
Vincent Beek 2-Mar-11 11:49am    
I am afraid that i get the same result.

messagebox only pops up after i close the 3th party application
Venkatesh Mookkan 2-Mar-11 11:52am    
Only reason I could see is, the 3rd party application is locking the file writing or reading. But have no idea why it is opening in notepad.
Vincent Beek 2-Mar-11 12:56pm    
I dont know iether. Thanks for the help anyway
Venkatesh Mookkan 2-Mar-11 21:08pm    
Down-voted! For what? Answering your question. Thanks buddy.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900