FileSystem.GetAttr(String) 方法

定義

傳回 FileAttribute 值,表示檔案、目錄或資料夾的屬性。 相較於 FileAttributeMy 功能可提升檔案 I/O 作業的產能和效能。 如需詳細資訊,請參閱FileSystem

public:
 static Microsoft::VisualBasic::FileAttribute GetAttr(System::String ^ PathName);
public static Microsoft.VisualBasic.FileAttribute GetAttr (string PathName);
static member GetAttr : string -> Microsoft.VisualBasic.FileAttribute
Public Function GetAttr (PathName As String) As FileAttribute

參數

PathName
String

必要。 指定檔案、目錄或資料夾名稱的字串運算式。 PathName 可包含目錄或資料夾,以及磁碟機。

傳回

列舉值的位元組合。

例外狀況

Pathname 無效或包含萬用字元。

目標檔案不存在。

範例

此範例會使用 函 GetAttr 式來判斷檔案和目錄或資料夾的屬性。

Dim MyAttr As FileAttribute
' Assume file TESTFILE is normal and readonly.
MyAttr = GetAttr("C:\TESTFILE.txt")   ' Returns vbNormal.

' Test for normal.
If (MyAttr And FileAttribute.Normal) = FileAttribute.Normal Then
    MsgBox("This file is normal.")
End If

' Test for normal and readonly.
Dim normalReadonly As FileAttribute
normalReadonly = FileAttribute.Normal Or FileAttribute.ReadOnly
If (MyAttr And normalReadonly) = normalReadonly Then
    MsgBox("This file is normal and readonly.")
End If

' Assume MYDIR is a directory or folder.
MyAttr = GetAttr("C:\MYDIR")
If (MyAttr And FileAttribute.Directory) = FileAttribute.Directory Then
    MsgBox("MYDIR is a directory")
End If

備註

若要判斷要設定的屬性,請使用 And 運算元,對函式所 GetAttr 傳回的值以及您想要的個別檔案屬性值執行位比較。 如果結果不是零,則會為具名檔案設定該屬性。 例如,如果未Archive設定屬性,則下列And表達式的傳回值為零:

Result = GetAttr(FName) And vbArchive  

如果已設定屬性, Archive 則會傳回非零值。

適用於

另請參閱