vb中怎么检查字符串是否为合法的文件路径
vb中如何检查字符串是否为合法的文件路径?
例如:str= "c:\www\w.txt "
str2= "c....\www\w.txt "
可能www文件夹和w.txt文件都不存在,但str合法,str2不合法
------解决方案--------------------
可以自己根据文件的命名规则来写,比如:
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
------解决方案--------------------
没必要这样复杂。在工程中引用Microsoft Scriping Runtime,然后定义:
dim fso As New Scripting.FileSystemObject
if fso.FolderExists(sPath) then
msgbox "路径存在 "
else
msgbox "路径不存在 "
end if
------解决方案--------------------
顶暴风雨老大的,我再补充:假定文件存在的情况下,这个是标准答案。
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
------解决方案--------------------
这种问题还是用错误处理代码来处理比较方便
否则的话逻辑很复杂,长文件名的问题、网络路径等等都需要考虑
private sub ....
on error goto fileopen_err
....
exit sub
fileopen_err:
msgbox err.description & chr(10) & "文件不能打开! "
例如:str= "c:\www\w.txt "
str2= "c....\www\w.txt "
可能www文件夹和w.txt文件都不存在,但str合法,str2不合法
------解决方案--------------------
可以自己根据文件的命名规则来写,比如:
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
------解决方案--------------------
没必要这样复杂。在工程中引用Microsoft Scriping Runtime,然后定义:
dim fso As New Scripting.FileSystemObject
if fso.FolderExists(sPath) then
msgbox "路径存在 "
else
msgbox "路径不存在 "
end if
------解决方案--------------------
顶暴风雨老大的,我再补充:假定文件存在的情况下,这个是标准答案。
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
------解决方案--------------------
这种问题还是用错误处理代码来处理比较方便
否则的话逻辑很复杂,长文件名的问题、网络路径等等都需要考虑
private sub ....
on error goto fileopen_err
....
exit sub
fileopen_err:
msgbox err.description & chr(10) & "文件不能打开! "