Export (0) Print
Expand All
2 out of 5 rated this helpful - Rate this topic

FileShare Enumeration

Contains constants for controlling the kind of access other FileStreams can have to the same file.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

[Visual Basic]
<Flags>
<Serializable>
Public Enum FileShare
[C#]
[Flags]
[Serializable]
public enum FileShare
[C++]
[Flags]
[Serializable]
__value public enum FileShare
[JScript]
public
   Flags
 Serializable
enum FileShare

Remarks

For an example of creating a file and writing text to a file, see Writing Text to a File. For an example of reading text from a file, see Reading Text from a File. For an example of reading from and writing to a binary file, see Reading and Writing to a Newly Created Data File.

A typical use of this enumeration is to define whether two processes can simultaneously read from the same file. For example, if a file is opened and FileShare.Read is specified, other users can open the file for reading but not for writing.

A FileShare parameter is specified in some of the constructors for FileStream, IsolatedStorageFileStream, and in some of the Open methods of File and FileInfo to control how a file is opened.

Members

Member name Description Value
Inheritable Makes the file handle inheritable by child processes. This is not directly supported by Win32. 16
None

Supported by the .NET Compact Framework.

Declines sharing of the current file. Any request to open the file (by this process or another process) will fail until the file is closed. 0
Read

Supported by the .NET Compact Framework.

Allows subsequent opening of the file for reading. If this flag is not specified, any request to open the file for reading (by this process or another process) will fail until the file is closed. However, if this flag is specified additional permissions might still be needed to access the file. 1
ReadWrite

Supported by the .NET Compact Framework.

Allows subsequent opening of the file for reading or writing. If this flag is not specified, any request to open the file for writing or reading (by this process or another process) will fail until the file is closed. However, if this flag is specified additional permissions might still be needed to access the file. 3
Write

Supported by the .NET Compact Framework.

Allows subsequent opening of the file for writing. If this flag is not specified, any request to open the file for writing (by this process or another process) will fail until the file is closed. However, if this flag is specified additional permissions might still be needed to access the file. 2

Example

The following FileStream constructor opens an existing file and grants read-only access to other users (FileShare.Read).

[Visual Basic] 
Dim s2 As New FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read)

[C#] 
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

[C++] 
FileStream* s2 = new FileStream(name, FileMode::Open, FileAccess::Read, FileShare::Read);

[JScript] 
var s2 : FileStream = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);

Requirements

Namespace: System.IO

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.IO Namespace | File.Open | FileInfo.Open | FileStream | IsolatedStorageFileStream | Working with I/O | Reading Text from a File | Writing Text to a File | Basic File I/O | Reading and Writing to a Newly Created Data File

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.