明晨网络

电话: 136-6532-7492 QQ: 给我发送消息 8507-0741

VB中使用API判断文件及目录的存在性

明晨网络原创,2009-06-12 18:49, 文章标签: VB 文件存在

    vb中判断文件存在可以使用这段代码来判断
 

代码如下
  1. Function Exists(Byval FilePath as String)as Boolean
  2.     If Dir(FilePath,16)<>"" Then Exists=True
  3. End Function

    
不过,貌似使用API更靠谱(测试)


 

代码如下
  1. '========================================
  2. '声明作用:文件存在性判断
  3. '========================================
  4. Private Const OFS_MAXPATHNAME = 128
  5. Private Const OF_EXIST = &H4000
  6.    
  7. Private Type OFSTRUCT
  8.     cBytes As Byte
  9.     fFixedDisk As Byte
  10.     nErrCode As Integer
  11.     Reserved1 As Integer
  12.     Reserved2 As Integer
  13.     szPathName(OFS_MAXPATHNAME) As Byte
  14. End Type
  15.    
  16. Private typOfStruct As OFSTRUCT
  17. Private Declare Function apiOpenFile Lib "kernel32" Alias "OpenFile" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
  18.    
  19.    
  20. '========================================
  21. '函数名称:Exists
  22. '函数作用:文件、夹存在性判断
  23. '========================================
  24. Public Function Exists(ByVal sFilename As String) As Boolean
  25.     On Error Resume Next
  26.     If Len(sFilename) > 0 Then
  27.         apiOpenFile sFilename, typOfStruct, OF_EXIST
  28.         Exists = typOfStruct.nErrCode <> 2
  29.     End If

文章源自:明晨网络,明晨网络原创,《VB中使用API判断文件及目录的存在性》,http://www.mingchennet.com/tec/code/vb/26.htm