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()

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
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

返回

一个字符串,表示与指定的模式或文件属性匹配的文件、目录或文件夹的名称,或驱动器的卷标签。

示例

此示例使用 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 函数进行后续调用。

Important

若要正确运行,该Dir函数需要Read授予执行代码的标志和标志。PathDiscoveryFileIOPermission 有关详细信息,请参阅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)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
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

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 返回驱动器的卷标签,而不是特定文件名。 必须首次提供 PathName 调用函数 Dir 的时间。 若要检索下一项,可以对没有参数的 Dir 函数进行后续调用。

Important

若要正确运行,该Dir函数需要Read授予执行代码的标志和标志。PathDiscoveryFileIOPermission 有关详细信息,请参阅FileIOPermissionSecurityException代码访问权限Attributes参数枚举值如下所示: |值 |常量 |说明| |-|-|-| |Normal|vbnormal|默认。 指定没有属性的文件。||ReadOnly|vbReadOnly|除了没有属性的文件之外,还指定只读文件。||Hidden|vbHidden|除了没有属性的文件之外,还指定隐藏的文件。||System|vbSystem|除了没有属性的文件之外,还指定系统文件。||Volume|vbVolume|指定卷标签;如果指定了任何其他属性,vbVolume则将被忽略。| |Directory|vbDirectory|除了没有属性的文件之外,还指定目录或文件夹。||Archive|vbArchive|自上次备份以来文件已更改。||Alias|vbAlias|文件具有其他名称。|

注释

这些枚举由Visual Basic语言指定,可在代码中的任何位置使用,以代替实际值。

另请参阅

适用于