Testing for invalid characters when creating a directory?

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?
 
C

Carl Daniel [VC++ MVP]

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
 
D

Dave

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top