HOW TO:在 Visual Basic 中判斷目錄屬性
GetDirectoryInfo方法會傳回 DirectoryInfo 物件,您可以查詢其 Attributes 屬性以判斷目錄的相關資訊。
下表列出了由 Attributes 屬性所使用之 FileAttributes 列舉型別的成員。
成員 |
數值 |
描述 |
---|---|---|
ReadOnly |
1 |
檔案是唯讀的。 |
Hidden |
2 |
檔案為隱藏的,因此不會包含在一般的目錄清單中。 |
System |
4 |
檔案是系統檔案。 檔案是作業系統的一部分,或由作業系統獨佔使用。 |
Directory |
16 |
檔案是目錄。 |
Archive |
32 |
檔案的保存 (Archive) 狀態。 應用程式會使用這個屬性,標記要備份或移除的檔案。 |
Device |
64 |
不適用。 |
Normal |
128 |
檔案為標準檔案,並未設定其他屬性。 此屬性只有在單獨使用時有效。 |
Temporary |
256 |
檔案是暫存的。 檔案系統會嘗試將所有資料放入記憶體中,以便更快速存取。 不再需要暫存檔案時,應加以刪除。 |
SparseFile |
512 |
檔案是疏鬆檔案。 疏鬆檔案通常是包含資料大部分為零的大型檔案。 |
ReparsePoint |
1024 |
檔案包含重新分析點,它是與檔案或目錄關聯之使用者定義的資料區塊。 |
Compressed |
2048 |
檔案已壓縮。 |
Offline |
4096 |
檔案為離線狀態,無法立即使用資料。 |
NotContentIndexed |
8192 |
作業系統的內容索引服務將不會編製檔案的索引。 |
Encrypted |
16384 |
檔案或目錄已加密。 對於檔案而言,這表示檔案中的所有資料都已加密。 對於目錄而言,這表示新建立的檔案和目錄都會預設加密。 |
若要判斷目錄是否隱藏
使用 GetDirectoryInfo 方法,傳回 DirectoryInfo 物件。 這個範例會傳回目錄 TestDir 的 DirectoryInfo,從 DirectoryInfo 物件取得 FileAttributes 物件,並加以檢查以判斷目錄是否隱藏。 您可以用類似的方式測試其他屬性。
Dim checkFile As System.IO.DirectoryInfo checkFile = My.Computer.FileSystem.GetDirectoryInfo("C:\TestDir") Dim attributeReader As System.IO.FileAttributes attributeReader = checkFile.Attributes If (attributeReader And System.IO.FileAttributes.Hidden) > 0 Then MsgBox("Directory is hidden") End If