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. 只在設定屬性 (property) 值時擲回。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- 使用者嘗試設定屬性 (attribute) 值,但並沒有寫入權限。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.

這個屬性的值是 archive、 compressed、 目錄、 隱藏、 離線、 唯讀的系統和暫存檔案的屬性旗標的組合。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# 或OrVisual 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

適用於

另請參閱