I just used this most useful link: How to check if a given line is a legal / valid file name under Windows?
And inside some verification code there is something similar (ignore the fact that I do not use the StringBuilder class and ignore the error when composing the message (you donโt need to tell them about โColonโ โโmore than once if it appears in the line 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; }
This MessageBox will look something like this (using the example in which the colon is found):
- to begin -
The following invalid characters were found:
:
- end -
I would like to say something like:
- to begin -
The following invalid characters were found:
Colon โ:
- end -
I like to have an English name. Not a killer, but it was curious if there was any function there (which does not exist for the Char class, but may exist in some other class that I do not think about):
Char.GetEnglishName (':');
Justooking
source share