Share via


HOW TO:在 Visual Basic 中判斷檔案是否為隱藏

更新:2007 年 11 月

My.Computer.FileSystem.GetFileInfo 方法可以用於取得 FileInfo 物件,此物件會包含指定之檔案的相關資訊,包括 FileAttributes 列舉型別 (Enumeration)。

若要判斷檔案是否隱藏

  1. 針對您要檢查的檔案,取得 FileInfo 物件。這個範例會取得檔案 Testfile.txt 的 FileInfo 物件。

    Dim infoReader As System.IO.FileInfo
    infoReader = My.Computer.FileSystem.GetFileInfo("C:\testfile.txt")
    
  2. FileInfo 物件取得 FileAttributes 物件。這個範例會從 FileInfo 物件取得 FileAttributes

    Dim attributeReader As System.IO.FileAttributes
    attributeReader = infoReader.Attributes
    
  3. 查詢 FileAttributes 以判斷檔案是否隱藏。這個範例會判斷檔案是否隱藏,並據此顯示結果。

    If (attributeReader And System.IO.FileAttributes.Hidden) > 0 Then
        MsgBox("File is hidden!")
    Else
        MsgBox("File is not hidden!")
    End If
    

請參閱

工作

HOW TO:在 Visual Basic 中判斷檔案屬性

參考

My.Computer.FileSystem 物件

My.Computer.FileSystem Object 成員

My.Computer.FileSystem.GetFileInfo 方法

其他資源

Visual Basic 中的檔案、目錄和磁碟機屬性