How to lock file

前端 未结 2 1773
清歌不尽
清歌不尽 2020-12-06 10:03

please tell me how to lock file in c#

Thanks

2条回答
  •  生来不讨喜
    2020-12-06 10:45

    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.

提交回复
热议问题