FileShare 枚举

定义

包含用于控制其他 FileStream 对象对同一文件可以具有的访问类型的常数。Contains constants for controlling the kind of access other FileStream objects can have to the same file.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class FileShare
[System.Flags]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum FileShare
type FileShare = 
Public Enum FileShare
继承
FileShare
属性

字段

Delete 4

允许随后删除文件。Allows subsequent deleting of a file.

Inheritable 16

使文件句柄可由子进程继承。Makes the file handle inheritable by child processes. Win32 不直接支持此功能。This is not directly supported by Win32.

None 0

谢绝共享当前文件。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.

Read 1

允许随后打开文件读取。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, even if this flag is specified, additional permissions might still be needed to access the file.

ReadWrite 3

允许随后打开文件读取或写入。Allows subsequent opening of the file for reading or writing. 如果未指定此标志,则文件关闭前,任何打开该文件以进行读取或写入的请求(由此进程或另一进程发出)都将失败。If this flag is not specified, any request to open the file for reading or writing (by this process or another process) will fail until the file is closed. 但是,即使指定了此标志,仍可能需要附加权限才能够访问该文件。However, even if this flag is specified, additional permissions might still be needed to access the file.

Write 2

允许随后打开文件写入。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, even if this flag is specified, additional permissions might still be needed to access the file.

示例

以下FileStream构造函数打开一个现有文件并向其他用户授予只读访问权限 (Read)。The following FileStream constructor opens an existing file and grants read-only access to other users (Read).

FileStream^ s2 = gcnew FileStream( name, FileMode::Open, FileAccess::Read, FileShare::Read );
FileStream s2 = new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
Dim s2 As New FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read)

注解

有关创建文件和将文本写入文件的示例, 请参阅如何:向文件写入文本。For an example of creating a file and writing text to a file, see How to: Write Text to a File. 有关从文件读取文本的示例, 请参阅如何:读取文件中的文本。For an example of reading text from a file, see How to: Read Text from a File. 有关从二进制文件读取和写入二进制文件的示例, 请参阅如何:读取和写入新创建的数据文件For an example of reading from and writing to a binary file, see How to: Read and Write 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. 例如, 如果文件已打开并Read已指定, 则其他用户可以打开该文件以进行读取, 但不能用于写入。For example, if a file is opened and Read is specified, other users can open the file for reading but not for writing.

FileStream File Open参数是在FileInfoIsolatedStorageFileStream和的一些构造函数中指定的, 并且在的某些方法中指定, 用于控制打开文件的方式。 FileShareA 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.

适用于

另请参阅