读取文字档 [英] Reading text files

查看:44
本文介绍了读取文字档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个在不同服务器上运行的Web应用程序,它将从nework文件夹中读取文本文件的内容.
我正在使用下面的代码读取该文件的contentf
FileStream fs = new FileStream("D:\\StatusFile.txt", FileMode.Open)

但是我正在获得system.io.ioexception

我已经修改了以下代码.

Hi ,
I have a web application running on different servers that will read content of a text file from the nework folder.
I am using below code to read contentf of that file
FileStream fs = new FileStream("D:\\StatusFile.txt", FileMode.Open)

But I am getting system.io.ioexception

I have mofified the code like below.

FileStream fs = new FileStream("D:\\StatusFile.txt", FileMode.Open,FileAccess.Read)



我的应用程序正在工作文件中.但是一些后台作业将写入此文本文件,但写入失败,我如何编写此应用程序的代码以读取文本内容,而又不影响后台作业来写入此文件.

请建议

谢谢,
Salmon.



My application is working file . but some back jobs will write to this text file, which are failing to write , how can i code this application to read text contents without affecting backend jobs to writing this files.

Please suggest

Thanks,
Salmon.

推荐答案

Hello Simon,

您可以使用其他共享选项打开文件.更多信息在这里:
http://msdn.microsoft.com/en-us/library/d0y914c5.aspx [ ^ ]

编写过程总是有可能发生冲突,因此您必须为此做好准备.因此,您应该在放弃之前尝试几次,例如:

Hello Simon,

You could open the file using extra sharing options. More info here:
http://msdn.microsoft.com/en-us/library/d0y914c5.aspx[^]

There is always a chance that to write processes collide and you have to be ready for that. So you should try a few times before giving up, something like:

int tryCount = 10;
bool haveFileAccess = false;
while (tryCount-- > 0) 
{ 
   haveFileAccess  = tryOpenFile(); 
   sleep(20);
}



请记住,最重要的是将文件访问权限限制在绝对最小值.完成操作后立即关闭文件,因为否则它会保持锁定状态,以供其他实例更新.

另一个更复杂的解决方案是创建一个监视目录的服务,并将带有创建指令的任何文件处理到该文件中.但是我想这有点过头了.

您也可以考虑使用数据库.编写可以非常安全地进行,并且阅读程序不会因此而烦恼.他们可以通过将多个记录连接在一起来重建任何数据,以获得与单个文件相同的结果.

祝你好运!



The most important to keep in mind is to keep file access to the absolute minimum. Close the file as soon as you''re done with it because it would otherwise keep it locked for other instances to update it.

Another more sophisticated solution would be to create a service that monitors a directory and any file with instructions created is processed into the file. But I guess this is something a bit over the top.

You could also consider using a database. Writing could be done very safely and the reader processes are not bothered by this. They can reconstruct any data by concatenating multiple records together to get the same result as a single file would.

Good luck!


这篇关于读取文字档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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