Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lock file

please tell me how to lock file in c#

Thanks

like image 845
Naruto Avatar asked Sep 16 '09 09:09

Naruto


1 Answers

Simply open it exclusively:

using (FileStream fs = 
         File.Open("MyFile.txt", FileMode.Open, FileAccess.Read, FileShare.None))
{
   // use fs
}

Ref.

Update: In response to comment from poster: According to the online MSDN doco, File.Open is supported in .Net Compact Framework 1.0 and 2.0.

like image 185
Mitch Wheat Avatar answered Sep 21 '22 05:09

Mitch Wheat