dim AppPath as string AppPath=app.path if right(AppPath,1)<>"\" then AppPath=AppPath & "\" end if if dir(AppPath & "a.txt")="" then msgbox "a.text 不存在!" else msgbox "a.text 存在!" end fi
if dir (app.path & "\1.txt") <> "" then msgbox "文件存在" end if
Const 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 Long Public Function FileExists(ByVal sFilename As String) As Boolean On Error Resume Next If Len(sFilename) > 0 Then apiOpenFile sFilename, typOfStruct, OF_EXIST FileExists = typOfStruct.nErrCode <> 2 End If End FunctionAPI的方法,很稳定的
'参数strFileName给出完整路径名称 Function blnFileExist(strFileName As String) As Boolean Dim strDir As String strDir = Dir(strFileName) If strDir = "" Then blnFileExist = False Else blnFileExist = True End If End Function 更多信息参考Dir函数
AppPath=app.path
if right(AppPath,1)<>"\" then
AppPath=AppPath & "\"
end if
if dir(AppPath & "a.txt")="" then
msgbox "a.text 不存在!"
else
msgbox "a.text 存在!"
end fi
msgbox "文件存在"
end if
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 Long
Public Function FileExists(ByVal sFilename As String) As Boolean
On Error Resume Next
If Len(sFilename) > 0 Then
apiOpenFile sFilename, typOfStruct, OF_EXIST
FileExists = typOfStruct.nErrCode <> 2
End If
End FunctionAPI的方法,很稳定的
Function blnFileExist(strFileName As String) As Boolean
Dim strDir As String
strDir = Dir(strFileName)
If strDir = "" Then
blnFileExist = False
Else
blnFileExist = True
End If
End Function
更多信息参考Dir函数