Check if filename is valid (Win32)
-9
A function to determine if the input filename is valid on Win32 platforms (that is, it does not include invalid characters, such as :\?...)
using System.Text;
using System.Text.RegularExpressions;
/// <summary>
/// Check if filename is valid on Win32 platforms (does not contain invalid characters)
/// </summary>
/// <param name="inputFileName">Name of the input file.</param>
/// <returns>
/// <c>true</c> if input filename is valid otherwise, <c>false</c>.
/// </returns>
public bool IsFilenameValid(string inputFileName)
{
Match m = Regex.Match(inputFileName, @"[\\\/\:\*\?\" + Convert.ToChar(34) + @"\<\>\|]");
return !(m.Success);
}






Bobby R Ward
---------------------
bobbyrward@gmail.com
public bool IsFilenameValid(string inputFileName)
{
return inputFileName.IndexOfAny(System.IO.Path.InvalidPathChars);
}
The original IsFilenameValid using the Regex function is more useful if you are validating a filename that the user has input (such as when creating a file).
.Net 2 provides the Path.GetInvalidFileNameChars method that can be used instead of the System.IO.Path.InvalidPathChars property to make a method similar to that suggested by bobbyrward.