Can I prevent a StreamReader from locking a text file whilst it is in use?

后端 未结 2 1157
灰色年华
灰色年华 2020-12-09 10:01

The StreamReader locks a text file whilst it is reading it.
Can I force the StreamReader to work in a \"read-only\" or \"non locking\" mode?

My workaround would

2条回答
  •  半阙折子戏
    2020-12-09 10:14

    You can pass a FileStream to the StreamReader, and create the FileStream with the proper FileShare value. For instance:

    using (var file = new FileStream (openFileDialog1.FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
    using (var reader = new StreamReader (file, Encoding.Unicode)) {
    }
    

提交回复
热议问题