Testing for invalid characters when creating a directory?

  • Thread starter Thread starter Dave
  • Start date Start date
D

Dave

Is there an easy way to test a directory name for invalid characters before
trying to create that directory? I know how to get a character array of
invalid characters (Path.GetInvalidPathChars) but do I have to go to all of
the trouble of writing of my validation method or is there something built
in to the .NET Framework that I haven't found yet?
 
Dave said:
Is there an easy way to test a directory name for invalid characters
before trying to create that directory? I know how to get a
character array of invalid characters (Path.GetInvalidPathChars) but
do I have to go to all of the trouble of writing of my validation
method or is there something built in to the .NET Framework that I
haven't found yet?

string path = "....";
if (path.IndexOfAny(Path.GetInvalidPathChars()) >= 0)
{
// path contains invalid characters
}

-cd
 
Thanks. Originally I was thinking that I wanted a method to eliminate
invalid characters from the path string (the string was actually the name of
something else unrelated to a path) but now I think I'll just prohibit those
characters from the original string.
 
Back
Top