FileSystem.Dir 方法

定義

回傳一個字串,代表與特定模式或檔案屬性相符的檔案、目錄或資料夾名稱,或磁碟機的磁碟區標籤。 它 FileSystem 能讓你在檔案 I/O 操作上的生產力和效能比功能更好 Dir 。 如需相關資訊,請參閱 GetDirectoryInfo(String)

多載

名稱 Description
Dir()

回傳一個字串,代表與特定模式或檔案屬性相符的檔案、目錄或資料夾名稱,或磁碟機的磁碟區標籤。 它 FileSystem 能讓你在檔案 I/O 操作上的生產力和效能比功能更好 Dir 。 如需相關資訊,請參閱 GetDirectoryInfo(String)

Dir(String, FileAttribute)

回傳一個字串,代表與特定模式或檔案屬性相符的檔案、目錄或資料夾名稱,或磁碟機的磁碟區標籤。 它 FileSystem 能讓你在檔案 I/O 操作上的生產力和效能比功能更好 Dir 。 如需相關資訊,請參閱 GetDirectoryInfo(String)

Dir()

回傳一個字串,代表與特定模式或檔案屬性相符的檔案、目錄或資料夾名稱,或磁碟機的磁碟區標籤。 它 FileSystem 能讓你在檔案 I/O 操作上的生產力和效能比功能更好 Dir 。 如需相關資訊,請參閱 GetDirectoryInfo(String)

public:
 static System::String ^ Dir();
public static string Dir();
static member Dir : unit -> string
Public Function Dir () As String

傳回

代表與特定模式或檔案屬性相符的檔案、目錄或資料夾名稱的字串,或磁碟的磁碟區標籤。

範例

這個範例使用了 Dir 這個函式來檢查某些檔案和目錄是否存在。

Dim MyFile, MyPath, MyName As String
' Returns "WIN.INI" if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")

' Returns filename with specified extension. If more than one *.INI
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again without arguments to return the next *.INI file in the
' same directory.
MyFile = Dir()

' Return first *.TXT file, including files with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)

' Display the names in C:\ that represent directories.
MyPath = "c:\"   ' Set the path.
MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
Do While MyName <> ""   ' Start the loop.
    ' Use bitwise comparison to make sure MyName is a directory.
    If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
        ' Display entry only if it's a directory.
        MsgBox(MyName)
    End If
    MyName = Dir()   ' Get next entry.
Loop

備註

Dir 函式支援使用多字元(*)和單字元(?)萬用字元來指定多個檔案。

VbVolume 會回傳磁碟機的磁碟區標籤,而不是特定的檔案名稱。

你必須在第一次打電話Dir時提供。PathName 要取得下一個項目,你可以在沒有參數的情況下對該函式進行後續呼叫 Dir

Important

要正確執行,Dir函式需要Read將 的 FileIOPermissionPathDiscovery 旗標授予執行中的程式碼。 欲了解更多資訊,請參閱 FileIOPermissionSecurityException程式碼存取權限

Attributes參數列舉值如下:

價值 常數 Description
Normal vbnormal 預設值。 指定沒有屬性的檔案。
ReadOnly vbReadOnly 指定唯讀檔案,以及無屬性的檔案。
Hidden vbHidden 指定隱藏檔案,以及沒有屬性的檔案。
System vbSystem 指定系統檔案,以及沒有屬性的檔案。
Volume vbVolume 指定體積標籤;若指定其他屬性, vbVolume 則忽略。
Directory vbDirectory 指定目錄或資料夾,以及沒有屬性的檔案。
Archive vbArchive 檔案自上次備份以來有變動。
Alias vbAlias 檔案名稱不同。

備註

這些列舉由 Visual Basic 語言規範,且可在程式碼中任意使用,取代實際值。

另請參閱

適用於

Dir(String, FileAttribute)

回傳一個字串,代表與特定模式或檔案屬性相符的檔案、目錄或資料夾名稱,或磁碟機的磁碟區標籤。 它 FileSystem 能讓你在檔案 I/O 操作上的生產力和效能比功能更好 Dir 。 如需相關資訊,請參閱 GetDirectoryInfo(String)

public static string Dir(string Pathname, Microsoft.VisualBasic.FileAttribute Attributes = Microsoft.VisualBasic.FileAttribute.Normal);
public static string Dir(string PathName, Microsoft.VisualBasic.FileAttribute Attributes = Microsoft.VisualBasic.FileAttribute.Normal);
static member Dir : string * Microsoft.VisualBasic.FileAttribute -> string
static member Dir : string * Microsoft.VisualBasic.FileAttribute -> string
Public Function Dir (Pathname As String, Optional Attributes As FileAttribute = Microsoft.VisualBasic.FileAttribute.Normal) As String
Public Function Dir (PathName As String, Optional Attributes As FileAttribute = Microsoft.VisualBasic.FileAttribute.Normal) As String

參數

PathnamePathName
String

Optional. 一個字串表達式,用來指定檔案名稱、目錄或資料夾名稱,或磁碟磁碟區標籤。 若Pathname找不到,則返回長度為零的字串("")。

Attributes
FileAttribute

Optional. 列舉或數值表達式,其值指定檔案屬性。 若省略,則回 Dir 傳符合 Pathname 但無屬性的檔案。

傳回

代表與特定模式或檔案屬性相符的檔案、目錄或資料夾名稱的字串,或磁碟的磁碟區標籤。

範例

這個範例使用了 Dir 這個函式來檢查某些檔案和目錄是否存在。

Dim MyFile, MyPath, MyName As String
' Returns "WIN.INI" if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")

' Returns filename with specified extension. If more than one *.INI
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again without arguments to return the next *.INI file in the
' same directory.
MyFile = Dir()

' Return first *.TXT file, including files with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)

' Display the names in C:\ that represent directories.
MyPath = "c:\"   ' Set the path.
MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
Do While MyName <> ""   ' Start the loop.
    ' Use bitwise comparison to make sure MyName is a directory.
    If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
        ' Display entry only if it's a directory.
        MsgBox(MyName)
    End If
    MyName = Dir()   ' Get next entry.
Loop

備註

Dir 函式支援使用多字元(*)和單字元(?)萬用字元來指定多個檔案。 VbVolume 會回傳磁碟機的磁碟區標籤,而不是特定的檔案名稱。 你必須在第一次打電話Dir時提供。PathName 要取得下一個項目,你可以在沒有參數的情況下對該 Dir 函式進行後續呼叫。

Important

要正確執行,Dir函式需要Read將 的 FileIOPermissionPathDiscovery 旗標授予執行中的程式碼。 欲了解更多資訊,請參閱 FileIOPermissionSecurityException程式碼存取權限Attributes參數列舉值如下: |價值|常數|說明| |-|-|-| || |Normal|vbnormal預設。 指定無屬性的檔案。|ReadOnly|vbReadOnly|指定唯讀檔案,以及無屬性的檔案。||Hidden|vbHidden|指定隱藏檔案,以及沒有屬性的檔案。||System|vbSystem|指定系統檔案,以及無屬性的檔案。|Volume|vbVolume|指定體積標籤;若指定其他屬性,vbVolume則忽略。| ||DirectoryvbDirectory|指定目錄或資料夾,以及沒有屬性的檔案。|Archive|vbArchive|檔案自上次備份以來已變更。|Alias|vbAlias|檔案名稱不同。

備註

這些列舉由 Visual Basic 語言規範,可以在程式碼中任何地方取代實際值。

另請參閱

適用於