FileAttributes 列舉
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
提供檔案和目錄的屬性。
此列舉支援其成員值的位元組合。
public enum class FileAttributes
[System.Flags]
public enum FileAttributes
[System.Flags]
[System.Serializable]
public enum FileAttributes
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum FileAttributes
[<System.Flags>]
type FileAttributes =
[<System.Flags>]
[<System.Serializable>]
type FileAttributes =
[<System.Flags>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type FileAttributes =
Public Enum FileAttributes
- 繼承
- 屬性
欄位
| 名稱 | 值 | Description |
|---|---|---|
| None | 0 | |
| ReadOnly | 1 | 檔案是唯讀的。
|
| 2 | 檔案已隱藏,因此不包含在一般目錄清單中。
|
|
| System | 4 | 檔案是系統檔案。 也就是說,該檔案是作業系統的一部分,或是作業系統獨佔使用。 |
| Directory | 16 | 該文件是目錄。
|
| Archive | 32 | 此檔案標記為包含於增量備份操作中。 Windows 在修改檔案時會設定此屬性,備份軟體在增量備份處理時應會清除此屬性。 |
| Device | 64 | 保留供未來使用。 |
| Normal | 128 | 該檔案為標準檔案,沒有特殊屬性。 此屬性僅在單獨使用時有效。
|
| Temporary | 256 | 檔案是暫時的。 暫存檔案包含應用程式執行時所需但應用程式完成後不再需要的資料。 檔案系統會嘗試將所有資料保存在記憶體中,以便更快存取,而不是將資料沖回大容量儲存。 應用程式應該在不再需要時立即刪除暫存盤。 |
| SparseFile | 512 | 檔案是稀疏檔案。 稀疏檔案通常是資料大多由零組成的大型檔案。 |
| ReparsePoint | 1024 | 檔案包含重解析點,這是與檔案或目錄相關的自訂數據區塊。
|
| Compressed | 2048 | 檔案已壓縮。 |
| Offline | 4096 | 檔案已離線。 檔案的數據無法立即取得。 |
| NotContentIndexed | 8192 | 此檔案不會由操作系統的內容索引服務編製索引。 |
| Encrypted | 16384 | 檔案或目錄已加密。 對於檔案,這表示檔案中的所有數據都會加密。 對於目錄,這表示加密是新建立檔案和目錄的預設值。 |
| IntegrityStream | 32768 | 該檔案或目錄包含資料完整性支援。 當此值套用到檔案時,檔案中的所有資料串流皆有完整性支援。 當此值套用到目錄時,該目錄內的所有新檔案與子目錄預設都會包含完整性支援。 |
| NoScrubData | 131072 | 檔案或目錄會被排除在資料完整性掃描之外。 當此值套用到目錄時,預設情況下,該目錄內所有新檔案和子目錄都會被排除在資料完整性之外。 |
範例
以下範例說明如何取得檔案的屬性並檢查該檔案是否為唯讀。
using System;
using System.IO;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
FileAttributes attributes = File.GetAttributes("c:/Temp/testfile.txt");
if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
{
Console.WriteLine("read-only file");
}
else
{
Console.WriteLine("not read-only file");
}
}
}
}
open System.IO
let attributes = File.GetAttributes "c:/Temp/testfile.txt"
if attributes &&& FileAttributes.ReadOnly = FileAttributes.ReadOnly then
printfn "read-only file"
else
printfn "not read-only file"
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim attributes = File.GetAttributes("c:/Temp/testfile.txt")
If ((attributes And FileAttributes.ReadOnly) = FileAttributes.ReadOnly) Then
Console.WriteLine("read-only file")
Else
Console.WriteLine("not read-only file")
End If
End Sub
End Module
備註
你可以透過呼叫 File.GetAttributes 這個方法取得檔案和目錄的屬性,也可以透過呼叫 File.SetAttributes 該方法來設定它們。
使用此File方法無法改變物件的File.SetAttributes壓縮狀態。 相反地,你必須用壓縮工具或命名空間中的 System.IO.Compression 類別來壓縮檔案。
以下屬性不支援 Linux 與 macOS 上的 .NET Core :
- FileAttributes.Archive
- FileAttributes.Compressed
- FileAttributes.Device
- FileAttributes.Encrypted
- FileAttributes.IntegrityStream
- FileAttributes.NoScrubData
- FileAttributes.NotContentIndexed
- FileAttributes.Offline
- FileAttributes.SparseFile
- FileAttributes.System
- FileAttributes.Temporary
在 Unix 系統中,回 File.GetAttributes 傳的值包含 Hidden 一個名稱以句點開頭的檔案(「.」)。 在 macOS 上,你可以設定或開啟隱藏旗標。