The FileShare enum in .NET specifies the level of access other
FileStream objects have to a file that is already open. It controls how multiple processes or threads can read, write, or delete a file simultaneously.
Namespace
System.IO
Used in
When opening a file, such as with FileStream, File.Open, etc.
Common Syntax
FileStream fs = new FileStream("example.txt", FileMode.Open, FileAccess.Read, FileShare.Read);
FileShare Enum Values
Value
Description
None
No other process can access the file while it's open. (Default behavior if not specified.)
Read
Allows other processes to read the file.
Write
Allows other processes to write to the file.
ReadWrite
Allows other processes to read from and write to the file.
Delete
Allows other processes to delete the file.
Inheritable
Allows the file handle to be inherited by child processes. (Rarely used)
Example Use Case
using (FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read))
{
// You can read/write, and others can still read the file
}
Why It Matters
If you try to open a file that another process has locked with FileShare.None, your code will throw an
IOException. Choosing the appropriate FileShare value prevents this.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
The
FileShareenum in .NET specifies the level of access otherFileStreamobjects have to a file that is already open. It controls how multiple processes or threads can read, write, or delete a file simultaneously.Namespace
Used in
When opening a file, such as with
FileStream,File.Open, etc.Common Syntax
FileShareEnum ValuesNoneReadWriteReadWriteDeleteInheritableExample Use Case
Why It Matters
If you try to open a file that another process has locked with
FileShare.None, your code will throw anIOException. Choosing the appropriateFileSharevalue prevents this.