FileSystemInfo.Attributes 属性

定义

获取或设置当前文件或目录的特性。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

注解

如果FileSystemInfo对象的当前Attributes实例是从以下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). FileNotFoundException或者DirectoryNotFoundException , 只能在设置值时引发。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# Visual Basic Or中的或) 来应用多个值。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

适用于

另请参阅