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

屬性值

目前 FileAttributesFileSystemInfo

例外狀況

指定的檔案不存在。 只在設定屬性值時擲回。

指定的路徑無效。 例如,它位於未對應的磁碟機上。 只在設定屬性 (property) 值時擲回。

呼叫端沒有必要的權限。

僅限 .NET Core 和 .NET 5+:用戶嘗試設定屬性值,但沒有寫入許可權。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

呼叫端嘗試設定無效的檔案屬性 (Attribute)。

-或-

僅限 .NET Framework:使用者嘗試設定屬性 (attribute) 值,但並沒有寫入權限。

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;

適用於

另請參閱