How to check if a filepath contains Invalid characters

To check if a filepath contains Invalid characters you can use the following snippet.

Sample C#

public static bool IsValidFilename(string fileName)
{
     return Path.GetInvalidFileNameChars().All(invalidChar => fileName.Contains(invalidChar) != true);
}

Sample VB.NET

Public Shared Function IsValidFilename(fileName As String) As Boolean
     Return Path.GetInvalidFileNameChars().All(Function(invalidChar) fileName.Contains(invalidChar) <> True)
End Function

for more informations see the MSDN Path.GetInvalidFileNameChars Method

Please Share us:
Please rate this snippet

One thought on “How to check if a filepath contains Invalid characters

Leave a Reply