更新:2007 年 11 月
返回表示匹配指定模式或文件属性的文件名、目录名或文件夹名的字符串或返回驱动器卷标的字符串。
在文件 I/O 操作中,My.Computer.FileSystem 对象具有比 Dir 函数更高的效率和更好的性能。有关更多信息,请参见 My.Computer.FileSystem.GetDirectoryInfo 方法。
Public Overloads Function Dir() As String
' -or-
Public Overloads Function Dir( _
       ByVal PathName As String, _ 
       Optional ByVal Attributes As FileAttribute = FileAttribute.Normal _
) As String
参数
- PathName 
 可选。String 表达式,指定文件名、目录或文件夹名或驱动器卷标。如果没有找到 PathName,则返回零长度字符串 ("")。
- Attributes 
 可选项。其值指定文件属性的枚举或数值表达式。如果省略,则 Dir 返回匹配 PathName 但没有属性的文件。
设置
Attributes 参数枚举值如下:
| 值 | 常数 | 说明 | 
|---|---|---|
| Normal | vbnormal | 默认值。指定无属性文件。 | 
| ReadOnly | vbReadOnly | 指定只读文件以及无属性文件。 | 
| Hidden | vbHidden | 指定隐藏文件以及无属性文件。 | 
| System | vbSystem | 指定系统文件以及无属性文件。 | 
| Volume | vbVolume | 指定卷标;如果指定了任何其他属性,则忽略 vbVolume。 | 
| Directory | vbDirectory | 指定目录或文件夹以及无属性文件。 | 
| Archive | vbArchive | 自从上次备份后文件已更改。 | 
| Alias | vbAlias | 文件具有不同的名称。 | 
| .gif) 说明: | 
|---|
| 这些枚举由 Visual Basic 语言指定并且可以在代码中的任何位置使用以替换实际值。 | 
备注
Dir 函数支持多字符 (*) 和单字符 (?) 通配符的使用以指定多个文件。
VbVolume 返回驱动器的卷标而不是特定文件名。
第一次调用 Dir 函数时必须提供 PathName。若要检索下一项,可以随后调用无参数的 Dir 函数。
| .gif) 安全说明: | 
|---|
| 若要正确运行,Dir 函数需要向执行代码授予 FileIOPermission 的 Read 和 PathDiscovery 标志。有关更多信息,请参见 FileIOPermission、SecurityException 和代码访问权限。 | 
示例
此示例使用 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
智能设备开发人员说明
不支持此函数。
要求
**模块:**FileSystem
**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)