共享文件时流阅读器“停滞" [英] streamreader 'stalls' when sharing a file

查看:107
本文介绍了共享文件时流阅读器“停滞"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下:

由第3方应用程序创建的文本文件.另一个应用程序此时正在积极添加.它每1分钟左右分批执行一次.
我可以使用记事本打开文本文件,然后在其中查看所有数据.
如果我尝试修改它,记事本将不允许我保存它.我会说很正常.

现在,我使用下面的代码访问相同的文件.

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 />



我没有例外,但是仅在我关闭第3方应用程序后才会弹出消息框. :confused:
我的应用似乎停滞在readline上.如何解决这个问题?
我希望它继续,以便我可以读取整个文件.应该可以,因为记事本可以.



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.

推荐答案

我想,您正在尝试在使用中打开文件.

尝试以下块.可能有帮助.

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

Try the below block. May be it help.

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.


这篇关于共享文件时流阅读器“停滞"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆