2024年2月10日发(作者:)

问题:

在VB6中检测文件是否存在?

方法:

1.用Dir函数,如果函数返回为空字符串则文件不存在。

例:If Dir(C:") = "" Then

MsgBox "文件不存在。"

End If

注意: 文件名要写完整,包括扩展名.

使用Dir("文件名")判断,但是当主调函数也正在用dir并且后续使用没有结束时就会出错。

如果还要对目录或者驱动器进行其他操作:

Public Function FileIsExist(Filename As String, Optional Attribus As VbFileAttribute = vbArchive) As Boolean

Dim cName As String

cName = Dir(Filename, Attribus)

FileIsExist = (Format(cName, ">") = Format(StripFile(Filename), ">"))

End Function

Public Function StripFile(Name As String) As String

Dim pos As Integer, pos1 As Integer

pos = InStr(Name, "")

While pos <> 0

If pos <> 0 Then pos1 = pos

pos = InStr(pos + 1, Name, "")

Wend

StripFile = Right(Name, Len(Name) - pos1)

End Function

2.用FileLen(pathname)函数,该函数返回一个 Long,代表一个文件的长度,单位是字节。

pathname 参数是用来指定一个文件名的字符串表达式。pathname 可以包含目录或文件夹、以及驱动器。

例:

If FileLen("TESTFILE") then

MsgBox "文件存在。"

End if

注意: 文件名要写完整,包括扩展名.

如果参数为目录名,则运行时刻出错.

3.用FileExists 方法

先在“引用”对话框中选中Microsoft Scripting Runtime

声明

例:

Dim fso As FileSystemObject

Set fso = CreateObject("stemObject")

If ists( & "") = False Then

MsgBox "缺少文件!", vbInformation

End If

注意:使用FileSystemObject(fso).程序发布时要带runtime文件,防止客户机器没有这个文件这样程序变成多个文件,很多操作系统。

4.最好的方法:

Public Function FileExists(ByVal File As String) As Boolean

On Error Resume Next

If (GetAttr(File) And vbDirectory) = False Then

FileExists = True

If err Then FileExists = False:

End Function

Function FolderExists(ByVal Folder As String) As Boolean

On Error Resume Next

If GetAttr(Folder) And vbDirectory Then

FolderExists = True

If err Then FolderExists = False:

End Function

上面都是用的vbDirectory=16 不要认为写错了

5.还有一个

'*************************************************

'**模 块 名:mod_GetFileExists

'**说 明:丹心软件在线设计 版权所有2007 - 2008(C)

'**创 建 人:丹心

'**日 期:2007-11-14 21:23:17

'**版 本:V1.0.0

'**博客地址:/starwork/

'**QQ 号码:121877114

'**E - mail:cnstarwork@

'*************************************************

Option Explicit

'api_GetPATH:vb中检测文件是否存在

Private Declare Function SHFileExists Lib "shell32" Alias "#45"

(ByVal szPath As String) As Long

'这个函数除了能判断文件是否存在外,还可以判断本地或远程文件夹

'"192.168.0.2d$"

'""

'"/"

Private Declare Function GetFileAttributes Lib "kernel32" Alias

"GetFileAttributesA" (ByVal lpFileName As String) As Long

Private Declare Function PathFileExists Lib "" Alias

"PathFileExistsA" (ByVal pszPath As String) As Long

'自定义全部属性,以检测隐藏或系统文件

Private Const vbAllFileAttrib = vbNormal + vbReadOnly +

vbHidden + vbSystem + vbVolume + vbDirectory

Private Enum SelectCheckFile

DIRCheck = 0 '方法1,Dir检测文件是否存在

OPENCheck = 1 '方法2,Open检测文件是否存在

GetFileCheck = 2 '判断指定文件的属性

PathFileCheck = 3 '判断文件是否存在外

SHFileExistsCheck = 4

End Enum

Public Function GetFileExists(filename As String, Index As

Integer) As Boolean

Dim fileIN As String, apiRet As Long

On Error GoTo FileDoesNotExist

If filename = "" Then GetFileExists = False: Exit Function

GetFileExists = False

Select Case Index

Case DIRCheck

fileIN = Dir$(filename, vbAllFileAttrib)

If fileIN <> vbNullString Then GetFileExists = True

Case OPENCheck

Open filename For Input As #1

Close #1

GetFileExists = True

Case GetFileCheck

apiRet& = Str$(GetFileAttributes(filename)) '返回负一则文件不存在

If Not apiRet < 0 Then GetFileExists = True

Case PathFileCheck '返回0则文件不存在

GetFileExists = CBool(PathFileExists(filename))

Case SHFileExistsCheck

If SHFileExists(filename) <> 0 Then GetFileExists =

True

Case Else

GetFileExists = False

End Select

Exit Function

FileDoesNotExist:

GetFileExists = False '文件不存在跳转

End Function

''test

'Private Sub Command1_Click()

'MsgBox GetFileExists( & "", 1)

'MsgBox GetFileExists( & "", 2)

'MsgBox GetFileExists( & "", 3)

'MsgBox GetFileExists( & "", 4)

'MsgBox GetFileExists( & "", 0)

'End Sub