开发者

Proper methods to have multiple processes reading the same file?

开发者 https://www.devze.com 2022-12-21 02:47 出处:网络
I have many processes reading a file stored on a network sha开发者_C百科re. Originally I was only able to have one process read the file, all the others would throw exceptions. I implemented the follo

I have many processes reading a file stored on a network sha开发者_C百科re. 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.


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.


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消