'/////////////////////////////////////////////////////////////////////
'//名称: IsFileExist
'//功能: judge the file exist or not
'//入口: FilePath, FileName
'//出口:
'//返回值:
' True: Exist
' False:Nonexist
'/////////////////////////////////////////////////////////////////////
Public Function IsFileExist(FilePath As String, Optional ByVal FileName As String) As Boolean
Dim strFile As String
If FileName <> "" Then
strFile = FilePath & "\" & FileName
Else
strFile = FilePath
FileName = GetFileName(FilePath)
End If
If UCase(Dir(strFile)) = UCase(FileName) Then
IsFileExist = True
Else
IsFileExist = False
End If
End Function
用这个就是你说的那种方法,打开一个文件如果出错哪就是文件不存在
Set Fos = CreateObject("Scripting.FileSystemObject")
Set Fil = Fos.createtextfile("Config.ini", True)
fos为file system object
config.ini为你要打开的文件
'-----------------------------------------------------------
' FUNCTION: FileExists
' Determines whether the specified file exists
'
' IN: [strPathName] - file to check for
'
' Returns: True if file exists, False otherwise
'-----------------------------------------------------------
'
Public Function FileExists(ByVal strPathName As String) As Boolean
Dim intFileNum As Integer
On Error Resume Next
'
' If the string is quoted, remove the quotes.
'
strPathName = strUnQuoteString(strPathName)
'
'Remove any trailing directory separator character
'
If Right$(strPathName, 1) = gstrSEP_DIR Then
strPathName = Left$(strPathName, Len(strPathName) - 1)
End If
'
'Attempt to open the file, return value of this function is False
'if an error occurs on open, True otherwise
'
intFileNum = FreeFile
Open strPathName For Input As intFileNum
FileExists = (Err.Number = 0)
Close intFileNum
Err.Clear
End Function
Public Function strUnQuoteString(ByVal strQuotedString As String)
'
' This routine tests to see if strQuotedString is wrapped in quotation
' marks, and, if so, remove them.
'
strQuotedString = Trim$(strQuotedString)
If Mid$(strQuotedString, 1, 1) = gstrQUOTE Then
If Right$(strQuotedString, 1) = gstrQUOTE Then
'
' It's quoted. Get rid of the quotes.
'
strQuotedString = Mid$(strQuotedString, 2, Len(strQuotedString) - 2)
End If
End If
strUnQuoteString = strQuotedString
End Function