So, I was wondering if you intend to use methods like CopyToAsync, should you open the underlying FileStream as shown above, and not do something simple like File.Open ?
I used ILSpy to decompile and looked at File.Open .
public static FileStream Open(string path, FileMode mode) { return File.Open(path, mode, (mode == FileMode.Append) ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None); }
What is called:
public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share) { return new FileStream(path, mode, access, share); }
And this particular FileStream constructor goes false for the useAsync parameter. So yes, that seems to matter. However, you can still use the async API, and it will still work the way you expected.
As Hans Passant says:
The base call to CreateFile() uses the FILE_FLAG_OVERLAPPED option. This allows overlapping I / O , a mechanism that allows asynchronous read and write at the winapi level.
The FileStream class has _isAsync bool, and that means: "If async IO is not supported on this platform or if this FileStream was not opened using FileOptions.Asynchronous.".
Again, you still get Task , which is an asynchronous operation of your choice.
source share