共用方式為


FileSystem.Dir 方法

定義

傳回的字串表示符合指定模式或檔案屬性的檔案、目錄或資料夾的名稱,也可以是磁碟機的磁碟區標籤。 FileSystem 提供比 Dir 函式更優越的檔案 I/O 作業產能和效能。 如需相關資訊,請參閱 GetDirectoryInfo(String)

多載

Dir()

傳回的字串表示符合指定模式或檔案屬性的檔案、目錄或資料夾的名稱,也可以是磁碟機的磁碟區標籤。 FileSystem 提供比 Dir 函式更優越的檔案 I/O 作業產能和效能。 如需相關資訊,請參閱 GetDirectoryInfo(String)

Dir(String, FileAttribute)

傳回的字串表示符合指定模式或檔案屬性的檔案、目錄或資料夾的名稱,也可以是磁碟機的磁碟區標籤。 FileSystem 提供比 Dir 函式更優越的檔案 I/O 作業產能和效能。 如需相關資訊,請參閱 GetDirectoryInfo(String)

Dir()

來源:
FileSystem.vb
來源:
FileSystem.vb
來源:
FileSystem.vb

傳回的字串表示符合指定模式或檔案屬性的檔案、目錄或資料夾的名稱,也可以是磁碟機的磁碟區標籤。 FileSystem 提供比 Dir 函式更優越的檔案 I/O 作業產能和效能。 如需相關資訊,請參閱 GetDirectoryInfo(String)

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

傳回

表示檔案、目錄或資料夾名稱的字串,該字串符合指定的模式或檔案屬性 (Attribute),也可以是磁碟機的磁碟區標籤 (Label)。

範例

此範例會使用函 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 會傳回磁碟驅動器的磁碟區標籤,而不是特定的檔名。

您第一 PathName 次呼叫函 Dir 式時必須提供 。 若要擷取下一個專案,您可以對函式進行後續呼叫, Dir 而不需要參數。

重要

若要正確執行,函 Dir 式需要 Read 將和 PathDiscovery 旗標 FileIOPermission 授與執行中的程序代碼。 如需詳細資訊,請參閱 FileIOPermissionSecurityException程式代碼訪問許可權

Attributes 變數列舉值如下所示:

常數 描述
Normal vbnormal 預設值。 指定不含屬性的檔案。
ReadOnly vbReadOnly 指定唯讀檔案,以及不含屬性的檔案。
Hidden vbHidden 指定隱藏的檔案,以及不含屬性的檔案。
System vbSystem 指定系統檔案,以及不含屬性的檔案。
Volume vbVolume 指定磁碟區標籤;如果指定任何其他屬性, vbVolume 則會忽略 。
Directory vbDirectory 指定目錄或資料夾,以及不含屬性的檔案。
Archive vbArchive 前次備份之後,檔案已經有了變更。
Alias vbAlias 檔案有不同的名稱。

注意

這些列舉是由 Visual Basic 語言所指定,而且可以在程式代碼中的任何位置使用,而不是實際值。

另請參閱

適用於

Dir(String, FileAttribute)

來源:
FileSystem.vb
來源:
FileSystem.vb
來源:
FileSystem.vb

傳回的字串表示符合指定模式或檔案屬性的檔案、目錄或資料夾的名稱,也可以是磁碟機的磁碟區標籤。 FileSystem 提供比 Dir 函式更優越的檔案 I/O 作業產能和效能。 如需相關資訊,請參閱 GetDirectoryInfo(String)

[System.Runtime.Versioning.SupportedOSPlatform("windows")]
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);
public static string Dir (string Pathname, Microsoft.VisualBasic.FileAttribute Attributes = Microsoft.VisualBasic.FileAttribute.Normal);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
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

選擇性。 字串運算式,可指定檔名、目錄或資料夾名稱或磁碟機的磁碟區標籤。 如果找不到 PathName,則會傳回長度為零的字串 ("")。

Attributes
FileAttribute

選擇性。 列舉類型或數值運算式,其值可指定檔案屬性。 如果省略,則 Dir 會傳回符合 Pathname 的檔案,但是沒有屬性。

傳回

表示檔案、目錄或資料夾名稱的字串,該字串符合指定的模式或檔案屬性 (Attribute),也可以是磁碟機的磁碟區標籤 (Label)。

屬性

範例

此範例會使用函 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 會傳回磁碟驅動器的磁碟區標籤,而不是特定的檔名。
您第一 PathName 次呼叫函 Dir 式時必須提供 。 若要擷取下一個專案,您可以對沒有參數的 Dir 函式進行後續呼叫。

重要

若要正確執行,函 Dir 式需要 Read 將和 PathDiscovery 旗標 FileIOPermission 授與執行中的程序代碼。 如需詳細資訊,請參閱 FileIOPermissionSecurityException程式代碼訪問許可權
Attributes 變數列舉值如下所示:
|Value|常數|描述|
|-|-|-|
|Normal|vbnormal|預設。 指定沒有屬性的檔案。|
|ReadOnly|vbReadOnly|除了沒有屬性的檔案之外,指定唯讀檔案。|
|Hidden|vbHidden|除了沒有屬性的檔案之外,還會指定隱藏的檔案。|
|System|vbSystem|除了沒有屬性的檔案之外,還會指定系統檔案。|
|Volume|vbVolume|指定磁碟區標籤;如果指定了任何其他屬性, vbVolume 則會忽略 。|
|Directory|vbDirectory|除了沒有屬性的檔案之外,還會指定目錄或資料夾。|
|Archive|vbArchive|檔案自上次備份后已變更。|
|Alias|vbAlias|檔案的名稱不同。|

注意

這些列舉是由 Visual Basic 語言所指定,而且可以在程式代碼中的任何位置使用,以取代實際值。

另請參閱

適用於