FileSystemInfo.Attributes FileSystemInfo.Attributes FileSystemInfo.Attributes FileSystemInfo.Attributes Property

定义

获取或设置当前文件或目录的特性。Gets or sets the attributes for the current file or directory.

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

属性值

异常

指定的文件不存在。The specified file doesn't exist. 仅在设置属性值时引发。Only thrown when setting the property value.

指定路径无效。The specified path is invalid. 例如,它位于未映射的驱动器上。For example, it's on an unmapped drive. 仅在设置属性值时引发。Only thrown when setting the property value.

调用方没有所需权限。The caller doesn't have the required permission.

调用方试图设置无效的文件属性。The caller attempts to set an invalid file attribute.

-or- 用户尝试设置属性值,但没有写入权限。The user attempts to set an attribute value but doesn't have write permission.

Refresh() 不能初始化数据。Refresh() cannot initialize the data.

示例

下面的示例演示Attributes属性。The following example demonstrates the Attributes property. 此代码示例是为提供一个更大示例的一部分FileSystemInfo类。This code example is part of a larger example provided for the FileSystemInfo class.

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

注解

Attributes属性会预先缓存,如果当前实例FileSystemInfo对象返回从以下任一DirectoryInfo方法:The value of the Attributes property is pre-cached if the current instance of the FileSystemInfo object was returned from any of the following DirectoryInfo methods:

值可以是缓存时可以为值本身或其他FileSystemInfo属性进行访问。The value may be cached when either the value itself or other FileSystemInfo properties are accessed. 若要获取最新值,请调用Refresh方法。To get the latest value, call the Refresh method.

如果该路径不存在从最后一个缓存状态开始,返回值是(FileAttributes)(-1)If the path doesn't exist as of the last cached state, the return value is (FileAttributes)(-1). FileNotFoundExceptionDirectoryNotFoundException仅可设置值时引发。FileNotFoundException or DirectoryNotFoundException can only be thrown when setting the value.

此属性的值是压缩的存档、 目录、 隐藏、 脱机、 只读的系统和临时文件的属性标志的组合。The value of this property is a combination of the archive, compressed, directory, hidden, offline, read-only, system, and temporary file attribute flags.

当设置此值时,使用按位 OR 运算符 (| C# 中或Or在 Visual Basic 中) 要应用多个值。When you set this value, use the bitwise OR operator (| in C# or Or in Visual Basic) to apply more than one value. 若要保留中的任何现有值Attributes属性,包含的值Attributes在分配中的属性。To retain any existing values in the Attributes property, include the value of the Attributes property in your assignment. 例如:For example:

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

安全性

FileIOPermission
用于写入文件和目录。for writing files and directories. 关联枚举:Write安全操作: DemandAssociated enumeration: Write Security action: Demand

适用于

另请参阅