FileStream with locked file

后端 未结 4 2050
悲&欢浪女
悲&欢浪女 2020-12-11 14:50

I am wondering if it\'s possible to get a readonly FileStream to a locked file? I now get an exception when I try to read the locked file.

using (FileStream          


        
4条回答
  •  轮回少年
    2020-12-11 15:43

    using (FileStream stream = new FileStream("path", FileMode.Open))
    

    That will use the default value for the FileShare argument, FileShare.Read. Which denies any process from writing to the file. That cannot work if another process is writing to the file, you cannot deny a right that was already gained.

    You have to specify FileShare.ReadWrite. That might still not work if the other process used FileShare.None, no workaround for that. Beware that getting read access to a file that's being written is troublesome, you don't have a reliable end-of-file indication. The last record or line in the file might have only been partially written.

提交回复
热议问题