The FileStream
constructor passes off to another constructor overload.. namely this one:
internal FileStream(string path, FileMode mode, FileAccess access, FileShare share, int bufferSize, FileOptions options, string msgPath, bool bFromProxy, bool useLongPath, bool checkHost)
The FileAccess
it passes by default is FileAccess.ReadWrite
. If your application does not have write permissions.. chances are this will fail.
On the other hand, File.ReadAllBytes
does this:
using (var fileStream = new FileStream(..., ..., FileAccess.Read, ...
// ^^^^ this
Notice File.ReadAllBytes
uses the FileAccess.Read
value.
So that's the difference. FileStream.ctor
uses FileAccess.ReadWrite
, File.ReadAllBytes
uses FileAccess.Read
.
TLDR: Your app needs write permissions to use that basic constructor for FileStream
. Remember there is a File.Exists
method that is meant for this exact purpose. Opening and closing a file to see if it exists is not the right way to go about it.