The constructor you are using for FileStream
defaults to sharing mode FileShare.Read
, meaning that later requests to open the file for reading (but not writing) will be allowed. This will work even when the file is already open for reading (if that is not exclusive reading).
On File.OpenWrite
, MSDN online states:
This method is equivalent to the FileStream(String, FileMode, FileAccess, FileShare) constructor overload with file mode set to OpenOrCreate, the access set to Write, and the share mode set to None.
http://msdn.microsoft.com/en-us/library/system.io.file.openwrite(v=vs.110).aspx
This means that File.OpenWrite
tries to get exclusive Read access to the file, not only exclusive Write access.
It sounds like when you test your file for "in use" it is open for reading.
Both methods check that the file is available for exclusive writing. You need to decide if your application also requires exclusive reading.