FileStream: used by another technology error

I have two different modules that need access to one file (One will have ReadWrite Access access - read-only for others). 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); 

The problem is that the second module crashes when you try 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 additional security parameters here?

+6
c # file-io filestream
source share
3 answers

In a FileStream that is only READING a file, you need to set it as

FileShare.ReadWrite

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

another wise source FileStream won't be able to write back to it ... its just a salvo back and forth between two streams, make sure you pass what others need

+22
source share

you need to use filestreamname.Open (); and filestreamname.close (); when using 2 streams that read / write to the same file, because you cannot read and write to the file asynchronously.

0
source share

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

0
source share

All Articles