Dim fs As Scripting.FileSystemObject '引用中的Scriptc的三个选项都选上,才可以使 Dim FileName As String Set fs = CreateObject("Scripting.FileSystemObject") If fs.FileExists(("c:\aa\bb.exe") = True Then '文件存在的操作了 End If
Attribute VB_Name = "ModuleExistFile" Option ExplicitConst OFS_MAXPATHNAME = 128 Const OF_EXIST = &H4000Private Type OFSTRUCT cBytes As Byte fFixedDisk As Byte nErrCode As Integer Reserved1 As Integer Reserved2 As Integer szPathName(OFS_MAXPATHNAME) As Byte End TypePrivate typOfStruct As OFSTRUCT Declare Function apiOpenFile Lib "kernel32" Alias "OpenFile" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As LongPublic Function Exists(ByVal sFilename As String) As Boolean On Error Resume Next If Len(sFilename) > 0 Then apiOpenFile sFilename, typOfStruct, OF_EXIST Exists = typOfStruct.nErrCode <> 2 End If End FunctionExists比dir可靠
endif
Dim FileName As String
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FileExists(("c:\aa\bb.exe") = True Then
'文件存在的操作了
End If
Option ExplicitConst OFS_MAXPATHNAME = 128
Const OF_EXIST = &H4000Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End TypePrivate typOfStruct As OFSTRUCT
Declare Function apiOpenFile Lib "kernel32" Alias "OpenFile" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As LongPublic Function Exists(ByVal sFilename As String) As Boolean
On Error Resume Next
If Len(sFilename) > 0 Then
apiOpenFile sFilename, typOfStruct, OF_EXIST
Exists = typOfStruct.nErrCode <> 2
End If
End FunctionExists比dir可靠