Share via


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

Reference

GetInvalidPathChars

Other Resources

Validating Strings in Visual Basic