加入microsoft scripting runtime(scrrun.dll)引用
Dim obj As New FileSystemObject
If obj.FolderExists("c:\temp") Then
'目录存在
End If
Set obj = Nothing
用以上代码判断即可
Private Sub Form_Load()
Dim f As String
f = Dir("c:\temp", vbDirectory)
If f <> "" Then MsgBox "c:\temp存在", , ""
If f = "" Then MsgBox "c:\temp不存在", , ""
End Sub
'The file you want to check
Const strFile = "C:\testfile.txt"
Private Sub Form_Load()
Dim msg As String
If ReportFileStatus(strFile) Then
msg = strFile & " exists."
Else
msg = strFile & " doesn't exist."
End If
MsgBox msg
End Sub
'The Method of FSO
'Add the Reference of Misrosoft Scripting Runtime
Function ReportFileStatus(filespec As String) As Boolean
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(filespec)) Then
ReportFileStatus = True
Else
ReportFileStatus = False
End If
End Function
'The Method of Dir
Function ReportFileStatus(filespec As String) As Boolean
Dim strdir As String
strdir = Dir(filespec)
If strdir <> "" Then
ReportFileStatus = True
Else
ReportFileStatus = False
End If
End Function