Difference between 'File.Open()' and 'new FileStream()'

None.

File.Open is, internally, nothing more than:

public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
{
    return new FileStream(path, mode, access, share);
}

If you don't use the overload which specifies a FileAccess and FileShare, it specifies this for you (using FileShare.None, and FileAccess.Write on append or ReadWrite otherwise).

That being said, this is an implementation detail, not part of the documentation. Technically, a future .NET Framework release could use a different implementation, although I find that unlikely.


This kind of duplication is very rare in the .NET framework. But there's a story about this one, told by Krzysztof Cwalina in this lecture. They did a usability study on an early version of the framework, asking a bunch of experienced (but otherwise .NET agnostic) programmers to write some code using the FileStream and StreadReader/Writer classes.

It didn't go well, they got a 100% fail rate. They responded by adding methods to the System.IO.File class, using the "most likely to fall into the pit of success" approach.

Cool video btw, if you're at all into the reasons the framework looks the way it looks.

Better post a real answer: the File.Open() method calls the FileStream constructor, passing values for FileAccess and FileShare (if you don't specify them) that are most likely to do the Right Thing. Which is FileAccess.ReadWrite and FileShare.None.


You have to be very careful with the answers given here! Because there is a subtle difference depending on the operating system you're using the method in.

It's due to the FileShare flag which declines sharing of the current file and pening will fail until the file is closed. The flag can optionally be passed to the constructor of new FileStream(...). On Windows machines IOException will always throw, on Linux machines only if the flag is set to FileShare.None. This is probably due to the difference in how files are handled on Linux (e.g. you can do crazy things like deleting a file on a Linux OS an still writing to it).

To make your code work on both Linux and Windows machines make sure you're either using the convenience function File.Open(...) which automatically sets the FileShare.None flag or pass the correct FileShare flag to the constructor.

I stumbled across that issue when using the following two versions to open a file:

var fileStreamSource = new FileStream(uploadFilePath, FileMode.Open);

and

var fileStreamSource = File.Open(uploadFilePath, FileMode.Open);

Former sets FileShare.Read, latter FileShare.None. They're idential on Windows machines but not on Linux machines.

I don't know if thats intentional or a bug. However, it bug me.


File.Open() is a convenience method. Internally it is implemented as:

public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share)
{
    return new FileStream(path, mode, access, share);
}

Tags:

C#

.Net

File Io