FileOptions 枚举


表示用于创建 FileStream 对象的高级选项。Represents advanced options for creating a FileStream object.

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

public enum class FileOptions
public enum FileOptions
type FileOptions = 
Public Enum FileOptions


Asynchronous 1073741824

指示文件可用于异步读取和写入。Indicates that a file can be used for asynchronous reading and writing.

DeleteOnClose 67108864

指示当不再使用某个文件时,自动删除该文件。Indicates that a file is automatically deleted when it is no longer in use.

Encrypted 16384

指示文件是加密的,只能通过用于加密的同一用户帐户来解密。Indicates that a file is encrypted and can be decrypted only by using the same user account used for encryption.

None 0

指示在生成 FileStream 对象时,不应使用其他选项。Indicates that no additional options should be used when creating a FileStream object.

RandomAccess 268435456

指示随机访问文件。Indicates that the file is accessed randomly. 系统可将此选项用作优化文件缓存的提示。The system can use this as a hint to optimize file caching.

SequentialScan 134217728

指示按从头到尾的顺序访问文件。Indicates that the file is to be accessed sequentially from beginning to end. 系统可将此选项用作优化文件缓存的提示。The system can use this as a hint to optimize file caching. 如果应用程序移动用于随机访问的文件指针,可能不发生优化缓存,但仍然保证操作的正确性。If an application moves the file pointer for random access, optimum caching may not occur; however, correct operation is still guaranteed. 如果指定此标志,可提升某些案例中的性能。Specifying this flag can increase performance in some cases.

WriteThrough -2147483648

指示系统应通过任何中间缓存、直接写入磁盘。Indicates that the system should write through any intermediate cache and go directly to disk.


下面的示例演示如何在创建文件流时使用异步值。The following example shows how to use the Asynchronous value when creating a file stream.

using System;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace ConsoleApplication
    class Program
        static void Main(string[] args)

        static async void WriteToFile()
            byte[] bytesToWrite = Encoding.Unicode.GetBytes("example text to write");
            using (FileStream createdFile = File.Create("c:/Temp/testfile.txt", 4096, FileOptions.Asynchronous))
                await createdFile.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
    End Sub

    Async Sub WriteToFile()
        Dim bytesToWrite = Encoding.Unicode.GetBytes("example text to write")
        Using createdFile As FileStream = File.Create("c:/Temp/testfile.txt", 4096, FileOptions.Asynchronous)
            Await createdFile.WriteAsync(bytesToWrite, 0, bytesToWrite.Length)
        End Using
    End Sub

End Module


FileOptions.SequentialScan指定标志可以提高使用顺序访问读取大型文件的应用程序的性能。Specifying the FileOptions.SequentialScan flag can increase performance for applications that read large files using sequential access. 对于经常按顺序读取大型文件的应用程序, 性能提升更为明显, 但偶尔会跳过小范围的字节。Performance gains can be even more noticeable for applications that read large files mostly sequentially, but occasionally skip over small ranges of bytes.