File being Used by Another process.

Posted by Kumar_jay99 under C# on 12/8/2011 | Points: 10 | Views : 2288 | Status : [Member] | Replies : 1
In C#, if 2 processes are reading and writing to the same file, what is the best way to avoid process locking exceptions?




Responses

Posted by: Kumar_jay99 on: 12/8/2011 [Member] Starter | Points: 25

Up
0
Down
There are two approach by which we can avoid this situation.

1.
using (FileStream stream = new FileStream(@"C:\Myfile.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
{
// Do your writing here.
}
2.
using (FileStream stream = new FileStream(@"C:\Myfile.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
// Does reading here.
}

Try this will helpful

Thanks

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response