开发者

FileStream: used by another process error

I have two different modules that need access to a single file (One will have ReadWrite Access - Other only Read). The file is opened using the following code in one of the modules:

FileStream fs1 = new FileStream(@"D:\post.xml", FileMode.Open, FileAccess.ReadWrite, FileShare.Read);

Th problem is that the second module fails while trying to open the same file using the following code:

FileStream fs = new FileStream(@"D:\post.xml", FileMode.Open, FileAccess.Read);

Do I need to set some开发者_如何学Python additional security parameters here?


On the FileStream that only READS the file, you need to set it as

FileShare.ReadWrite

FileStream fs = new FileStream(@"D:\post.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

other wise the original FileStream would not be able to write back to it...its just a volley back and forth between the two streams, make sure you hand back what the other needs


When opening the second FileStream, you also need to specify FileShare.Read, otherwise it will try to open it with exclusive access, and will fail because the file is already open


you need to use the filestreamname.Open(); and the filestreamname.close(); command when using 2 filestreams that read/write to the same file, because you can't read and write to a file asynchronously.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜