VB中使用API判断文件及目录的存在性
vb中判断文件存在可以使用这段代码来判断
代码如下
-
Function Exists(Byval FilePath as String)as Boolean
-
If Dir(FilePath,16)<>"" Then Exists=True
-
End Function
不过,貌似使用API更靠谱(测试)
代码如下
-
'========================================
-
'声明作用:文件存在性判断
-
'========================================
-
Private Const OFS_MAXPATHNAME = 128
-
Private Const OF_EXIST = &H4000
-
-
Private Type OFSTRUCT
-
cBytes As Byte
-
fFixedDisk As Byte
-
nErrCode As Integer
-
Reserved1 As Integer
-
Reserved2 As Integer
-
szPathName(OFS_MAXPATHNAME) As Byte
-
End Type
-
-
Private typOfStruct As OFSTRUCT
-
Private Declare Function apiOpenFile Lib "kernel32" Alias "OpenFile" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
-
-
-
'========================================
-
'函数名称:Exists
-
'函数作用:文件、夹存在性判断
-
'========================================
-
Public 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
文章源自:明晨网络,明晨网络原创,《VB中使用API判断文件及目录的存在性》,http://www.mingchennet.com/tec/code/vb/26.htm