首页 > 解决方案 > 两个控制台程序从 C# 中的同一文件中读取内容

问题描述

我已经编写了用于读取文件内容的控制台应用程序(文件位于共享网络上)。文件以读取模式打开并授予权限,如下所示,

FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader reader = new StreamReader(stream);            
var E51300Index = reader.ReadLine();

然后这个控制台应用程序安装在多台机器(共享网络)上,并尝试从不同的机器同时访问。

不允许并行访问及其抛出错误,

Process cannot access the file
xxx.txt because it is being used by another process

是否有任何解决文件不锁定/并行访问的方法。

提前致谢!

标签: c#multithreadinglockingsystem.io.file

解决方案


推荐阅读