vb中如何检查字符串是否为合法的文件路径?

wjgly0303 2007-04-17 11:39:31
例如:str="c:\www\w.txt"
str2="c....\www\w.txt"
可能www文件夹和w.txt文件都不存在,但str合法,str2不合法
...全文
1007 15 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
rainstormmaster 2007-04-17
  • 打赏
  • 举报
回复
可以自己根据文件的命名规则来写,比如:

Public Function IsFilePathValid(sFilePath As String, Optional bExists As Boolean = False) As Boolean

On Error GoTo handleError

sFilePath = Trim(sFilePath)
If sFilePath = "" Then
IsFilePathValid = False
Else
If Len(sFilePath) < 4 Then 'c:\t
IsFilePathValid = False
Else
If Mid(sFilePath, 2, 2) <> ":\" Then
IsFilePathValid = False
Else
If sFilePath Like "*\" Then
IsFilePathValid = False
Exit Function
Else
End If

If bExists Then
If GetAttr(sFilePath) And vbDirectory Then
IsFilePathValid = False
Exit Function
End If
If Not IsFileExists(sFilePath) Then
IsFilePathValid = False
Exit Function
End If
End If

IsFilePathValid = True
End If
End If
End If

Exit Function

handleError:
IsFilePathValid = False

End Function

DengXingJie 2007-04-17
  • 打赏
  • 举报
回复
正则表达式
guyehanxinlei 2007-04-17
  • 打赏
  • 举报
回复
用FSO
TechnoFantasy 2007-04-17
  • 打赏
  • 举报
回复
参考:
http://vbnet.mvps.org/index.html?code/fileapi/pathskiproot.htm
利用PathSkipRoot函数来分析。
rainstormmaster 2007-04-17
  • 打赏
  • 举报
回复
你举几个例子

其实方法很多,不一定非用正则,不如,你可以先判断文件是否存在,存在的话,肯定合法,不存在的话,你就以字串为文件名写入文件(可以要提高程序权限),然后利用错误陷阱判定
Rose2007 2007-04-17
  • 打赏
  • 举报
回复
这种问题还是用错误处理代码来处理比较方便
否则的话逻辑很复杂,长文件名的问题、网络路径等等都需要考虑

private sub ....
on error goto fileopen_err
....

exit sub
fileopen_err:
msgbox err.description & chr(10) & "文件不能打开!"
endsub


转自 fuxc(Michael(继续迷茫))
dljdljdlj10000 2007-04-17
  • 打赏
  • 举报
回复
怎么回事啊,我为何不能发帖子,真奇怪,????我不能进入提问的页面????
vbman2003 2007-04-17
  • 打赏
  • 举报
回复
除了正则表达式,用可以用like判断吧?
wjgly0303 2007-04-17
  • 打赏
  • 举报
回复
别外个人有个想法,但不知道怎么实现,请大家帮忙

首先不进行判断,直接进行创建文件夹或文件,如果正常创建,则删除刚刚建的文件,上述过程都不出错,返回true

如果建的过程出错,直接返回false,退出函数


提供给诸位高手参考
wjgly0303 2007-04-17
  • 打赏
  • 举报
回复
谢谢大家的热心,但本人想要的是合法不合法,并非存在不存在

上面的方法除了正则表达式,可能其它的都不太合适

谢谢暴风雨,但您的方法只能检查到错误的范围很小,很多时候不合法的情况是不能检查到的

欢迎大家继续讨论
rainstormmaster 2007-04-17
  • 打赏
  • 举报
回复
//没必要这样复杂

呵呵,注意审题:)
迈克揉索芙特 2007-04-17
  • 打赏
  • 举报
回复
顶暴风雨老大的,我再补充:假定文件存在的情况下,这个是标准答案。
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const INVALID_HANDLE_VALUE = -1
Function FileExists(ByVal FileName As String) As Boolean
Dim r As Long
r = GetFileAttributes(FileName)
FileExists = r <> INVALID_HANDLE_VALUE And (Not r And FILE_ATTRIBUTE_DIRECTORY)
End Function
rainstormmaster 2007-04-17
  • 打赏
  • 举报
回复
也可以用pathisurl、pathfileExists之类的api进行判定
lailuboy 2007-04-17
  • 打赏
  • 举报
回复
检测文件存不存在则:
fso.fileExist(sfile)
lailuboy 2007-04-17
  • 打赏
  • 举报
回复
没必要这样复杂。在工程中引用Microsoft Scriping Runtime,然后定义:
dim fso As New Scripting.FileSystemObject
if fso.FolderExists(sPath) then
    msgbox "路径存在"
else
msgbox "路径不存在"
end if

7,785

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧