Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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)


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.
Posted
Updated 5-Oct-10 23:16pm
v2
Comments
Ankur\m/ 6-Oct-10 5:41am    
Please do not add answer if you need to clarify something with the answerer. He is not notified. Instead use 'Add Comment' below his answer, like I did.

1 solution

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!
 
Share this answer
 
v2
Comments
Ankur\m/ 6-Oct-10 5:40am    
[moved from answer]:
salmonraju wrote:
Hi Nijboer,
I am using below code but not seeing any issue while updating files with other process.Please suggest. I have added FileShare.ReadWrite

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

Thanks,
Salmon.
E.F. Nijboer 6-Oct-10 8:44am    
I do not fully understand because you are not seeing any issue while reading the file. If there is no issue then it's alright. Probably there is something wrong. My guess, by seeing what you have changed, you also need to update the writer with a FileShare.Read because he must also allow it. Could you otherwise give more info?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900