I was just using this most helpful link: How do I check if a given string is a legal / valid file name under Windows?
And inside some validate code I have something that looks like (ignore the fact that I'm not using a StringBuilder class and ignore the bug in forming the message (don't need to tell them about 'Colon' more than once if it shows up in the string more than once)):
string InvalidFileNameChars = new string(Path.GetInvalidFileNameChars());
Regex ContainsABadChar = new Regex("[" + Regex.Escape(InvalidFileNameChars) + "]");
MatchCollection BadChars = ContainsABadChar.Matches(txtFileName.Text);
if (BadChars.Count > 0)
{
string Msg = "The following invalid characters were detected:\r\n\r\n";
foreach (Match Bad in BadChars)
{
Msg += Bad.Value + "\r\n";
}
MessageBox.Show(Msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
That MessageBox will look something like (using the example that a colon was found):
-- begin --
The following invalid characters are detected:
:
-- end --
I'd like it to say something like:
-- begin --
The following invalid characters are detected:
Colon -> :
-- end --
I like having the english name. Not a killer, but was curious if there's some function out there like (which doesn't exist for the Char class, but may exist in some other class I'm not thinking of):
Char.GetEnglishName(':');