2024年2月7日发(作者:)
VB 检查文件存在的三种方法_网路空间_百度空间
用户名: 密码: 登录
注册
网路空间
〈胡子软件工作室〉 /boxroom
主页博客相册|个人档案 |好友
查看文章
VB 检查文件存在的三种方法
2009年06月03日 下午 09:47
VB 检查文件存在的三种方法
━━━━━━━━━━━━━━━━━━━━━━━━━━
VB有个内置的函数: DIR
在指定的前题下,它可以为你提供文件列表.若你指明了文件的全名(和路径).如果文件存在,它将返回文件名.若指定文件不存在,返回的是一个空值.
找C盘下的
if dir("c:") <>"" then
msgbox "file exists~!"
else
msgbox" file doesn't exist!"
end if
若文件有特殊的属性(如隐藏),那么你也需要指明它.
if dir("c:",vbhidden) <> "" then
msgbox "file exists!"
else
msgbox " file doesn't exist!"
end if
这里有一个函数可以返回TRUE 或 false 如果文件存在或否.
private function CheckPath(srtPath as string) as boolean
if dir$(strPath) <> "" then
checkPath = True
else
checkPath = False
end if
end function
----------------------------------------------------------------------------------------------------------------------
最可靠的一种方法来检查文件是否存在(不论是其属性是隐藏,系统,或其它别的什么都不会影响到检查的结果...)
'In a standard Module:
Option Explicit
Private Const OF_EXIST As Long = &H4000
Private Const OFS_MAXPATHNAME As Long = 128
Private Const HFILE_ERROR As Long = -1
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 Declare Function OpenFile Lib "kernel32" (ByVal lpFileName
As String, _
lpReOpenBuff As OFSTRUCT, ByVal wStyle As
Long) As Long
Public Function FileExists(ByVal Fname As String) As Boolean
Dim lRetVal As Long
Dim OfSt As OFSTRUCT
lRetVal = OpenFile(Fname, OfSt, OF_EXIST)
If lRetVal <> HFILE_ERROR Then
FileExists = True
Else
FileExists = False
End If
End Function
'Usage:
'Behind Form1:
Option Explicit
Private Sub Form_Load()
MsgBox FileExists("C:") 'True :D
End Sub
类别:◆vb篇 | | 添加到搜藏 | 分享到i贴吧 | 浏览() | 评论 (0)
最近读者:
网友评论:
发表评论:
内 容:
取消回复
©2010 Baidu
本文发布于:2024-02-07 16:04:17,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170729305765381.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |