FileShare Enumeration

System.IO

Summary

Specifies the type of access a FileShare object has to a file.

Members

Read Read = 0

Allows subsequent opening of the file for reading. No other processes can open the file for writing.

Write Write = 1

Allows subsequent opening of the file for writing. No other processes can open the file for reading or writing.

ReadWrite ReadWrite = 2

Allows subsequent opening of the file for reading or writing. No other processes can open the file for reading or writing.

Delete Delete = 4

Allows subsequent opening and deletion of the file. No other processes can open the file for reading, writing, or deletion.

Inheritance Inheritance = 0x00010000

Specifies that the share mode applies to the descendants of the file.

None None = 0

Specifies that no other processes can open the file.

Note: This is the default value. The Read member has the same value.

Syntax

public enum FileShare { None = 0, Read = 0, Write = 1, ReadWrite = 2, Delete = 4, Inheritance = 0x00010000 }

Remarks

This enumeration is used by the constructors of the FileStream class to control how a file can be shared.

When you create a FileStream object, you can specify the share mode to indicate whether other processes can access the file while it is open.

For example, to open a file for reading and prevent other processes from writing to it, you would use:

using var fs = new FileStream("myFile.txt", FileMode.Open, FileAccess.Read, FileShare.Read);

If you want to allow other processes to read and write to the file while it's open, you can use:

using var fs = new FileStream("myFile.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

The None and Read values are equivalent, meaning that if you specify FileShare.None, other processes can still open the file for reading.