Read and write a FileStream at the same time

FileStream fs1 = new FileStream("TestFile", FileMode.Open, FileAccess.Write, FileShare.Read);        

FileStream fs2 = new FileStream("TestFile", FileMode.Open, FileAccess.Read, FileShare.Read);

IOException will be thrown about "The process cannot access the file ... because it is being used by another process."

Resolution:

Set FileShare parameter to Write or ReadWrite for fs2.
The FileShare param. specifies whether you want the file to be shared for Read, Write or Both, if you only specify Read, you can't open the file because fs1 has it open for Write.

發佈了72 篇原創文章 · 獲贊 0 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章