如何:在 Visual Basic 中验证文件名和路径
此示例返回一个 Boolean 值,该值指示字符串表示文件名还是路径。 验证操作检查名称是否包含文件系统不允许的字符。
示例
Function IsValidFileNameOrPath(ByVal name As String) As Boolean
' Determines if the name is Nothing.
If name Is Nothing Then
Return False
End If
' Determines if there are bad characters in the name.
For Each badChar As Char In System.IO.Path.GetInvalidPathChars
If InStr(name, badChar) > 0 Then
Return False
End If
Next
' The name passes basic validation.
Return True
End Function
此示例不会检查下列内容:名称中冒号的位置是否不正确、目录是否没有名称,或者名称的长度是否超过了系统定义的最大长度。 也不会检查应用程序是否有访问具有指定名称的文件系统资源的权限。