FileOptions 枚举

定义

表示用于创建 FileStream 对象的高级选项。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum FileOptions
C#
[System.Flags]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum FileOptions
继承
FileOptions
属性

字段

名称 说明
Asynchronous 1073741824

指示文件可用于异步读取和写入。

DeleteOnClose 67108864

指示当不再使用某个文件时,自动删除该文件。

Encrypted 16384

指示文件是加密的,只能通过用于加密的同一用户帐户来解密。

None 0

指示在生成 FileStream 对象时,不应使用其他选项。

RandomAccess 268435456

指示随机访问文件。 系统可将此选项用作优化文件缓存的提示。

SequentialScan 134217728

指示按从头到尾的顺序访问文件。 系统可将此选项用作优化文件缓存的提示。 如果应用程序移动用于随机访问的文件指针,可能不发生优化缓存,但仍然保证操作的正确性。 如果指定此标志,可提升某些案例中的性能。

WriteThrough -2147483648

指示系统应通过任何中间缓存、直接写入磁盘。

示例

以下示例演示如何在创建文件流时使用异步值。

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

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            WriteToFile();
        }

        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);
            }
        }
    }
}

注解

指定 FileOptions.SequentialScan 标志可以提高使用顺序访问读取大型文件的应用程序的性能。 对于主要按顺序读取大型文件,但偶尔跳过较小字节范围的应用程序,性能提升可能更为明显。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.3, 1.4, 1.6, 2.0, 2.1
UWP 10.0