How to: Validate File Names and Paths in Visual Basic
This example returns a Boolean
value that indicates whether a string represents a file name or path. The validation checks if the name contains characters that are not allowed by the file system.
Example
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
This example does not check if the name has incorrectly placed colons, or directories with no name, or if the length of the name exceeds the system-defined maximum length. It also does not check if the application has permission to access the file-system resource with the specified name.
See also
Tee yhteistyötä kanssamme GitHubissa
Tämän sisällön lähde on GitHubissa, jossa voit myös luoda ja tarkastella ongelmia ja pull-pyyntöjä. Katso lisätietoja osallistujan oppaasta.