FileAttributes 枚举


提供文件和目录的属性。Provides attributes for files and directories.

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

public enum class FileAttributes
public enum FileAttributes
type FileAttributes = 
Public Enum FileAttributes


Archive 32

此文件标记为包含在增量备份操作中。This file is marked to be included in incremental backup operation. 每当修改文件时,Windows 会设置该属性,并且在增量备份期间处理文件时,备份软件应进行清理该属性。Windows sets this attribute whenever the file is modified, and backup software should clear it when processing the file during incremental backup.

Compressed 2048

此文件是压缩文件。The file is compressed.

Device 64

留待将来使用。Reserved for future use.

Directory 16

此文件是一个目录。The file is a directory. Directory 在 Windows、Linux 和 macOS 上受支持。Directory is supported on Windows, Linux, and macOS.

Encrypted 16384

此文件或目录已加密。The file or directory is encrypted. 对于文件来说,表示文件中的所有数据都是加密的。For a file, this means that all data in the file is encrypted. 对于目录来说,表示新创建的文件和目录在默认情况下是加密的。For a directory, this means that encryption is the default for newly created files and directories.

Hidden 2

文件是隐藏的,因此没有包括在普通的目录列表中。The file is hidden, and thus is not included in an ordinary directory listing. Hidden 在 Windows、Linux 和 macOS 上受支持。Hidden is supported on Windows, Linux, and macOS.

IntegrityStream 32768

文件或目录包括完整性支持数据。The file or directory includes data integrity support. 在此值适用于文件时,文件中的所有数据流具有完整性支持。When this value is applied to a file, all data streams in the file have integrity support. 此值将应用于一个目录时,所有新文件和子目录在该目录中和默认情况下应包括完整性支持。When this value is applied to a directory, all new files and subdirectories within that directory, by default, include integrity support.

Normal 128

该文件是没有特殊属性的标准文件。The file is a standard file that has no special attributes. 仅当其单独使用时,此特性才有效。This attribute is valid only if it is used alone. Normal 在 Windows、Linux 和 macOS 上受支持。Normal is supported on Windows, Linux, and macOS.

NoScrubData 131072

文件或目录从完整性扫描数据中排除。The file or directory is excluded from the data integrity scan. 此值将应用于一个目录时,所有新文件和子目录在该目录中和默认情况下应不包括数据完整性。When this value is applied to a directory, by default, all new files and subdirectories within that directory are excluded from data integrity.

NotContentIndexed 8192

将不会通过操作系统的内容索引服务来索引此文件。The file will not be indexed by the operating system's content indexing service.

Offline 4096

此文件处于脱机状态,The file is offline. 文件数据不能立即供使用。The data of the file is not immediately available.

ReadOnly 1

文件为只读文件。The file is read-only. ReadOnly 在 Windows、Linux 和 macOS 上受支持。ReadOnly is supported on Windows, Linux, and macOS. 在 Linux 和 macOS 上,更改 ReadOnly 标记是权限操作。On Linux and macOS, changing the ReadOnly flag is a permissions operation.

ReparsePoint 1024

文件包含一个重新分析点,它是一个与文件或目录关联的用户定义的数据块。The file contains a reparse point, which is a block of user-defined data associated with a file or a directory. ReparsePoint 在 Windows、Linux 和 macOS 上受支持。ReparsePoint is supported on Windows, Linux, and macOS.

SparseFile 512

此文件是稀疏文件。The file is a sparse file. 稀疏文件一般是数据通常为零的大文件。Sparse files are typically large files whose data consists of mostly zeros.

System 4

此文件是系统文件。The file is a system file. 即,该文件是操作系统的一部分或者由操作系统以独占方式使用。That is, the file is part of the operating system or is used exclusively by the operating system.

Temporary 256

文件是临时文件。The file is temporary. 临时文件包含当执行应用程序时需要的,但当应用程序完成后不需要的数据。A temporary file contains data that is needed while an application is executing but is not needed after the application is finished. 文件系统尝试将所有数据保存在内存中,而不是将数据刷新回大容量存储,以便可以快速访问。File systems try to keep all the data in memory for quicker access rather than flushing the data back to mass storage. 当临时文件不再需要时,应用程序应立即删除它。A temporary file should be deleted by the application as soon as it is no longer needed.


下面的示例演示如何检索文件的特性并检查该文件是否为只读。The following example shows how to retrieve the attributes for a file and check if the file is read-only.

using System;
using System.IO;

namespace ConsoleApplication
    class Program
        static void Main(string[] args)
            FileAttributes attributes = File.GetAttributes("c:/Temp/testfile.txt");
            if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                Console.WriteLine("read-only file");
                Console.WriteLine("not read-only file");
Imports System.IO
Imports System.Text

Module Module1
    Sub Main()
        Dim attributes = File.GetAttributes("c:/Temp/testfile.txt")
        If ((attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly) Then
            Console.WriteLine("read-only file")
            Console.WriteLine("not read-only file")
        End If
    End Sub
End Module


可以通过调用 File.GetAttributes 方法来获取文件和目录的属性,并且可以通过调用 File.SetAttributes 方法来设置这些属性和目录。You can get attributes for files and directories by calling the File.GetAttributes method, and you can set them by calling the File.SetAttributes method.

不可能使用 File.SetAttributes 方法更改 File 对象的压缩状态。It is not possible to change the compression status of a File object by using the File.SetAttributes method. 相反,你必须使用压缩工具或 System.IO.Compression 命名空间中的某个类来实际压缩文件。Instead, you must actually compress the file using either a compression tool or one of the classes in the System.IO.Compression namespace.

Linux 和 macOS 上的 .NET Core 不支持以下属性:The following attributes are not supported by .NET Core on Linux and macOS:

在 Unix 系统上,File.GetAttributes 返回的值包含名称以句点(".")开头的文件 HiddenOn Unix systems, the value returned by File.GetAttributes includes Hidden for a file whose name begins with a period ("."). 在 macOS 上,可以获取或设置隐藏标志。On macOS, you can get or set the hidden flag.