Search code examples
filestream.net-5

FileStream constructor with FileOptions argument. Is this still valid .NET 5?


According to the .NET 5 documentation for the FileStream class, it still takes a constructor that permits the user to pass in a FileOptions argument.

FileStream(String, FileMode, FileAccess, FileShare, Int32, FileOptions) 

Yet in practice, the constructor does not seem to be there. Even when I navigate to decompiled sources, I don't see it there Does anyone know if this is a documentation oversight or am I missing something?

Note that I am building my .NET 5 application with a windows target, if that matters

<TargetFramework>net5.0-windows</TargetFramework>

Also, if it matters, this is what I'm trying to do (which does not build)

string path = Path.Combine(Folder, "temp-lock-delete-me.tmp");
_preventRenameFs = new FileStream(
    path, 
    FileAccess.ReadWrite,
    FileShare.Delete | FileShare.Write | FileShare.Read,
    4096,
    FileOptions.DeleteOnClose);

Solution

  • As per your code you need to add FileMode argument

    string path = Path.Combine(Folder, "temp-lock-delete-me.tmp");
    _preventRenameFs = new FileStream(
        path, 
        FileMode.OpenOrCreate,// <-- add FileMode
        FileAccess.ReadWrite,
        FileShare.Delete | FileShare.Write | FileShare.Read,
        4096,
        FileOptions.DeleteOnClose);