Accessing a text file for reading and writing at the same time..


Oct 1, 2009
Ok, i have two programs one program writes to a text file on a consistent basis, while the second program is opening this text file on a consistent basis for reading..

Sometimes the programs are accessing the file at the same time and the user keeps getting this error : this process cannot access the file because its already in use..

Is there any class, in that i can use to modify the settings of this text file so both programs can access it at the same time?

Any help would be appreciated!


Ultimate Contributor
Jun 10, 2003
This doesn't really have anything to do with the settings of the text file, per se, so much as how one program or another is using the text file. Normally, when a program opens a file, the file is locked so that no other program can open the file.

There is a straightforward way around this, but it requires that you access the file via a FileStream (i.e. you can't do this with methods such as File.ReadAllLines).
var fileStream = System.IO.File.Open(
The FileShare value specifies how the program is willing to share the file. You can allow other programs to read the file, or to write to the file, or both.

But before you slap file-access-sharing into your programs, first consider why .NET (or any self-respecting API) would default to locking a file. If one program is writing while the other is reading things can go terribly wrong. Things get sketchy when the file is being modified under your feet as you read it. The only way I would feel safe doing this is if the two programs were working together and communiticating what was going on to eachother, but if the two programs are communiticating to eachother they should be able to skip the file and send the info in question directly to eachother. Beyond that, when you share the file access, you can't pick and choose who else can access the file. It won't be limited to your other program, and that sounds a little sketchy to me, too.


Oct 1, 2009
Thanks for the info.. I agree to it can get complicated two programs accessing it at the same time.. At the time it's what the user wanted, but he has since changed his mind..

You reply hasn't been in vain tho.. I learned something new :)
Top Bottom