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. 只在設定屬性 (property) 值時擲回。Only thrown when setting the property value.

呼叫端沒有必要的權限。The caller doesn't have the required permission.

僅限 .NET Core:使用者嘗試設定屬性 (attribute) 值,但並沒有寫入權限。.NET Core only: The user attempts to set an attribute value but doesn't have write permission.

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。The specified path, file name, or both exceed the system-defined maximum length.

呼叫端嘗試設定無效的檔案屬性 (Attribute)。The caller attempts to set an invalid file attribute.

-或--or-

僅限 .NET Framework:使用者嘗試設定屬性 (attribute) 值,但並沒有寫入權限。.NET Framework only: 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 物件的目前實例是從下列任何一種 DirectoryInfo 方法傳回,則會預先快取 Attributes 屬性的值: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). 只有在設定值時,才會擲回 FileNotFoundExceptionDirectoryNotFoundExceptionFileNotFoundException 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;

適用於

另請參閱