views:

796

answers:

2

Is there a way to check if a String meant for a path has invalid characters, in .Net? I know I could iterate over each character in Path.InvalidPathChars to see if my String contained one, but I'd prefer a simple, perhaps more formal, solution.

Is there one?

I've found I still get an exception if I only check against Get

Update:

I've found GetInvalidPathChars does not cover every invalid path character. GetInvalidFileNameChars has 5 more, including '?', which I've come across. I'm going to switch to that, and I'll report back if it, too, proves to be inadequate.

Update 2:

GetInvalidFileNameChars is definitely not what I want. It contains ':', which any absolute path is going to contain ("C:\whatever"). I think I'm just going to have to use GetInvalidPathChars after all, and add in '?' and any other characters that cause me problems as they come up. Better solutions welcome.

+15  A: 

InvalidPathChars is deprecated. Use GetInvalidPathChars() instead:

    public static bool FilePathHasInvalidChars(string path)
    {

        return (!string.IsNullOrEmpty(path) && path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0);
    }

Edit: Slightly longer, but handles path vs file invalid chars in one function:

    // WARNING: Not tested
    public static bool FilePathHasInvalidChars(string path)
    {
        bool ret = false;
        if(!string.IsNullOrEmpty(path))
        {
            try
            {
                // Careful!
                //    Path.GetDirectoryName("C:\Directory\SubDirectory")
                //    returns "C:\Directory", which may not be what you want in
                //    this case. You may need to explicitly add a trailing \
                //    if path is a directory and not a file path. As written, 
                //    this function just assumes path is a file path.
                string fileName = System.IO.Path.GetFileName(path);
                string fileDirectory = System.IO.Path.GetDirectoryName(path);

                // we don't need to do anything else,
                                    // if we got here without throwing an 
                                    // exception, then the path does not
                                    // contain invalid characters
            }
            catch (ArgumentException)
            {
                                    // Path functions will throw this 
                                    // if path contains invalid chars
                ret = true;
            }
        }
        return ret;
    }
Jeremy Bell
I'm tired now (3AM) but methinks that IndexOfAny returns -1 if no invalid char is found, thus the result is true if NO such char is found in either filename or fileDirectory, exactly the opposite of what is wanted.But, more importantly, how does this solve "c:\first\second:third\test.txt"? Would it catch the second, illegal ':'?
Avi
See edits to original post.As to your other question, "C:\first\second:third\test.txt" does not contain any invalid characters for a path, since ":" is a valid path character. True, the path is an invalid path, but the purpose of the function was not to validate proper paths. For that, the best bet would be to test the path string against a regular expression.You could also do: foreach(String s in path.Split('\\')) {// test s for invalid file characters}but that implementation is a little brittle since you have to make an exception for the "C:"
Jeremy Bell
+1  A: 

have you tried using regualar expressions???

Create some sample regular expressions that match whole strings with specific illegal characters and create a few different scenerios where the sample string might cater for different contexts. Such a telephone based string, or a email based string. It should be easy to see how many different sample strings you can come up with that cater for your circumstances.

For example I used a simple regex string to match driver letters that were being pulled out of binary settings file. I create a regex expression to match the exact case I was looking for and ignore all other types.

In your case, try a switch to cater for the different sample strings that you might be catering for, where in each case block you could match different regex strings or the sample sample string on all case blocks but it being matched by different regex string. That represent the different illegal characters you need to look for.

I'm a noobie but that's what comes to mind for me. Sorry for no regex samples but I would take a look at it, the 70-536 training kit ebook from john Sharp has a nice primer on it.

Ibrar

IbrarMumtaz