FileSystemInfo.Attributes 属性

定义

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

public:
 property System::IO::FileAttributes Attributes { System::IO::FileAttributes get(); void set(System::IO::FileAttributes value); };
public System.IO.FileAttributes Attributes { get; set; }
member this.Attributes : System.IO.FileAttributes with get, set
Public Property Attributes As FileAttributes

属性值

FileAttributes

当前 FileAttributesFileSystemInfo

例外

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

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

调用方没有所需权限。

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

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

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

  • 或 -

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

Refresh() 不能初始化数据。

示例

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

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);
}
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);
}
Sub DisplayFileSystemInfoAttributes(ByVal fsi As IO.FileSystemInfo)
    ' Assume that this entry is a file.
    Dim entryType As String = "File"

    ' Determine if this entry is really a directory.
    If (fsi.Attributes And FileAttributes.Directory) = FileAttributes.Directory Then
        entryType = "Directory"
    End If

    ' Show this entry's type, name, and creation date.
    Console.WriteLine("{0} entry {1} was created on {2:D}", _
        entryType, fsi.FullName, fsi.CreationTime)
End Sub

注解

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

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

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

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

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

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

适用于

另请参阅