您的位置:首页 > 编程语言 > VB

VB中检测文件是否存在

2011-09-03 23:14 288 查看
问题:
在VB中检测文件是否存在?

方法:
1.用Dir函数,如果函数返回为空字符串则文件不存在。
例:If Dir(C:\Win95\My.txt") = "" 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("Scripting.FileSystemObject")
If fso.FileExists(App.path & "\Template.dat") = False Then
MsgBox "缺少Template.dat文件!", 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: err.Clear
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: err.Clear
End Function
上面都是用的vbDirectory=16 不要认为写错了

5.还有一个
'*************************************************************************
'**模 块 名:mod_GetFileExists
'**说 明:丹心软件在线设计 版权所有2007 - 2008(C)
'**创 建 人:丹心
'**日 期:2007-11-14 21:23:17
'**版 本:V1.0.0
'**博客地址:http://hi.baidu.com/starwork/
'**QQ 号码:121877114
'**E - mail:cnstarwork@126.com
'*************************************************************************
Option Explicit

'api_GetPATH:vb中检测文件是否存在
Private Declare Function SHFileExists Lib "shell32" Alias "#45" (ByVal szPath As String) As Long
'这个函数除了能判断文件是否存在外,还可以判断本地或远程文件夹
'"\\192.168.0.2\d$\javatools\somefile.txt"
'"\\workstation\javatools\somefile.txt"
'"http://www.microsoft.com/ms.htm"
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long

Private Declare Function PathFileExists Lib "shlwapi.dll" 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(App.Path & "\1.exe", 1)
'MsgBox GetFileExists(App.Path & "\1.exe", 2)
'MsgBox GetFileExists(App.Path & "\1.exe", 3)
'MsgBox GetFileExists(App.Path & "\1.exe", 4)
'MsgBox GetFileExists(App.Path & "\1.exe", 0)
'End Sub
http://hi.baidu.com/starwork/blog/item/b04d3c465760160a6a63e56e.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  vb 职场 文件