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.

呼叫端嘗試設定無效的檔案屬性 (Attribute)。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

備註

如果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# in Or或 in 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

適用於

另請參閱