views:

263

answers:

2

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.

+1  A: 

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.

Sean.C
Interesting solution. I kind of like it, but I don't think I'll get too many votes for that solution. However it does give me an idea since I do have a master program controlling all the processes, and that could possibly feed the processes the files they need... But eventually there will be multiple of those running as well, and I'd prefer to go for a guaranteed solution.
MGSoto
A: 

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