让多个进程读取同一文件的正确方法?

发布于 2024-08-23 04:34:30 字数 644 浏览 5 评论 0原文

我有许多进程正在读取存储在网络共享上的文件。最初我只能让一个进程读取该文件,所有其他进程都会抛出异常。我实现了以下代码来处理这个问题:

using (StreamReader fileStreamReader = new StreamReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
   content = fileStreamReader.ReadToEnd();
}

这让多个进程读取同一个文件,但是它似乎仍然存在问题,因为有时多个进程仍然无法访问该文件。但我可以稍后在文件未使用时返回并打开它。现在我有一些重试行为,并实施了随机延迟,到目前为止,似乎有帮助。我觉得这样做有点奇怪,那么有什么更好的方法呢?

这是奇怪的部分,我得到的异常根本不是来自文件 IO,而是来自一个名为 CommStudio 的库。简而言之,我将文件转储到一个字符串,稍微修改它,将其转储到内存流中,然后通过 rs232 上的 ymodem 将其发送出去。例外情况是告诉我远程系统已取消。获取数据的设备报告存在传输错误,这通常意味着接收到不完整/空文件。

通常我会将此归咎于库,但它在桌面测试和只有一个进程访问文件时完美地工作。唯一看起来真正一致的是,当多个进程访问一个文件时,它很可能会失败。

I have many processes reading a file stored on a network share. Originally I was only able to have one process read the file, all the others would throw exceptions. I implemented the following code to deal with that:

using (StreamReader fileStreamReader = new StreamReader(File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read)))
{
   content = fileStreamReader.ReadToEnd();
}

This let multiple processes read the same file, however it still seems to have issues, because sometimes multiple processes still can't access the file. Yet I can go back later when the file isn't in use and open it just fine. Right now I have some retry behavior with random delays implemented that so far, seem to help. It seems a little quirky to me to do it this way, so what would be a better method?

This is the weird part, the exception I'm getting is not from file IO at all, it's from a library called CommStudio. In short, I dump the file to a string, i modify it slightly, dump it into a memory stream, and ship it off over ymodem on rs232. The exception is telling me the remote system has canceled. The device getting the data reports that there was a transmission error, which usually means that an incomplete/empty file was received.

Normally I would blame the library on this, but it works flawlessly at desk-testing and when there is only one process accessing the file. The only thing that really seems to be consistent is that it is likely to fail when multiple processes are accessing a file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揪着可爱 2024-08-30 04:34:30

有类似的问题,但没有分配时间来找到理想的解决方案。我创建了一个 Web 服务并将文件粘贴到 Web 服务应用程序本地。然后创建了一个简单的单行 GET API,通过办公室内联网调用该 API。从而确保只有调用应用程序编辑了日志文件。虽然混乱但功能齐全。

had a similar problem but not allot of time to find an ideal solution. I created a webservice and stuck the file local to the webservice app.. then created a simple one liner GET API which was called over the office intranet.. thus ensureing only the calling application edited the log file.. messy but functional.

横笛休吹塞上声 2024-08-30 04:34:30

我过去也遇到过类似的问题。尝试将访问文件的方式更改为类似这样。

//Use FileInfo to get around OS locking of the file
FileInfo fileInfo = new FileInfo(path); 
//I actually wanted unblocked read write access so change your access and share appropriately
using (FileStream fs = fileInfo.Open(FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
    //I'm using CopyTo but use whatever method matches your need
    fileInfo.CopyTo(Path.Combine(destination, fileName), false);
}

I have had a similar problem in the past. Try changing how you access the file to something like this.

//Use FileInfo to get around OS locking of the file
FileInfo fileInfo = new FileInfo(path); 
//I actually wanted unblocked read write access so change your access and share appropriately
using (FileStream fs = fileInfo.Open(FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
{
    //I'm using CopyTo but use whatever method matches your need
    fileInfo.CopyTo(Path.Combine(destination, fileName), false);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文