FileSystem.Dir 方法

定义

返回表示文件、目录或文件夹名称的字符串,该文件、目录或文件夹具有指定的模式或文件特性,或者具有指定的驱动器卷标。 与 Dir 函数相比,FileSystem 在文件 I/O 操作方面可为您提供更高的效率和更好的性能。 有关更多信息,请参见GetDirectoryInfo(String)

重载

Dir()

返回表示文件、目录或文件夹名称的字符串,该文件、目录或文件夹具有指定的模式或文件特性,或者具有指定的驱动器卷标。 与 Dir 函数相比,FileSystem 在文件 I/O 操作方面可为您提供更高的效率和更好的性能。 有关更多信息,请参见GetDirectoryInfo(String)

Dir(String, FileAttribute)

返回表示文件、目录或文件夹名称的字符串,该文件、目录或文件夹具有指定的模式或文件特性,或者具有指定的驱动器卷标。 与 Dir 函数相比,FileSystem 在文件 I/O 操作方面可为您提供更高的效率和更好的性能。 有关更多信息,请参见GetDirectoryInfo(String)

Dir()

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

返回表示文件、目录或文件夹名称的字符串,该文件、目录或文件夹具有指定的模式或文件特性,或者具有指定的驱动器卷标。 与 Dir 函数相比,FileSystem 在文件 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 返回驱动器的卷标签,而不是特定的文件名。

必须在首次调用Dir函数时提供 PathName 。 若要检索下一项,可以在不带参数的情况下对 Dir 函数进行后续调用。

重要

若要正确运行, Dir 函数要求Read向执行代码授予 的 FileIOPermissionPathDiscovery 标志。 有关详细信息,请参阅 FileIOPermissionSecurityException代码访问权限

参数 Attributes 枚举值如下所示:

“值” 返回的常量 描述
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

返回表示文件、目录或文件夹名称的字符串,该文件、目录或文件夹具有指定的模式或文件特性,或者具有指定的驱动器卷标。 与 Dir 函数相比,FileSystem 在文件 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 匹配但不具有任何特性的文件。

返回

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

属性

示例

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

重要

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

注意

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

另请参阅

适用于