Re: Validation Expression by Nicole
Nicole
Wed Jan 05 11:39:53 CST 2005
The list of invalid path characters is contained in
System.IO.Path.InvalidPathCharacters. For a file name, you should also
exclude the four separators specified in the Path class (i.e.:
AltDirectorySeparatorChar, DirectorySeparatorChar, PathSeparator, and
VolumeSeparatorChar). One approach to testing for these characters would be
to contruct one array of the invalid path characters and the separator
characters, then use the string's IndexOfAny method to verify if the
proposed file name constains any character from the array. e.g.:
public bool ContainsInvalidFileCharacters(string target)
{
char[] invalidPathChars = Path.InvalidPathChars;
int i = invalidPathChars.Length;
char[] invalidFileChars = new char[i + 4];
Array.Copy(invalidPathChars, invalidFileChars, i);
invalidFileChars[i] = Path.AltDirectorySeparatorChar;
invalidFileChars[i + 1] = Path.DirectorySeparatorChar;
invalidFileChars[i + 2] = Path.PathSeparator;
invalidFileChars[i + 3] = Path.VolumeSeparatorChar;
return (target.IndexOfAny(invalidFileChars) >= 0);
}
"Marlon" <yardi4life@online.nospam> wrote in message
news:uTAYZc08EHA.2180@TK2MSFTNGP10.phx.gbl...
> Can someone help me with a validation to check for invalid characters in a
> filename ?
>