FileSystemInfo.Attributes 属性

定义

获取或设置当前文件或目录的特性。

C#
public System.IO.FileAttributes Attributes { get; set; }

属性值

当前 FileAttributesFileSystemInfo

例外

指定的文件不存在。 仅在设置属性值时引发。

指定路径无效。 例如,它位于未映射的驱动器上。 仅在设置属性值时引发。

调用方没有所需权限。

仅限 .NET Core 和 .NET 5+:用户尝试设置属性值,但没有写入权限。

指定的路径和/或文件名超过了系统定义的最大长度。

调用方试图设置无效的文件属性。

仅限 .NET Framework:用户尝试设置属性值,但没有写入权限。

Refresh() 不能初始化数据。

示例

以下示例演示 了 Attributes 属性。 此代码示例是为 FileSystemInfo 类提供的一个更大示例的一部分。

C#
static void DisplayFileSystemInfoAttributes(FileSystemInfo fsi)
{
    //  Assume that this entry is a file.
    string entryType = "File";

    // Determine if entry is really a directory
    if ((fsi.Attributes & FileAttributes.Directory) == FileAttributes.Directory )
    {
        entryType = "Directory";
    }
    //  Show this entry's type, name, and creation date.
    Console.WriteLine("{0} entry {1} was created on {2:D}", entryType, fsi.FullName, fsi.CreationTime);
}

注解

如果对象的当前实例FileSystemInfo是从以下DirectoryInfo任一方法返回的,则会预先缓存 属性的值Attributes

当访问值本身或其他 FileSystemInfo 属性时,可能会缓存该值。 若要获取最新值,请调用 Refresh 方法。

如果自上次缓存状态起路径不存在,则返回值为 (FileAttributes)(-1)FileNotFoundExceptionDirectoryNotFoundException 只能在设置值时引发。

此属性的值是存档、压缩、目录、隐藏、脱机、只读、系统和临时文件属性标志的组合。

设置此值时,请使用 C# 或 Or Visual Basic) | 中的按位 OR 运算符 (来应用多个值。 若要在 属性中 Attributes 保留任何现有值,请在赋值中包含 属性 Attributes 的值。 例如:

exampleFile.Attributes = exampleFile.Attributes | FileAttributes.ReadOnly;

适用于

产品 版本
.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 1.1, 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

另请参阅