0

I get a The path is not of a legal form error when trying to obtain

IO.Path.GetDirectoryName(myString)

... where (in this case) myString is an empty string.

It would be easy to test for an empty string first.

However is there a more comprehensive test against a string, so that this error can be avoided for all kinds of invalid myString?

hawbsl
  • 15,313
  • 25
  • 73
  • 114
  • Perhaps this will help http://stackoverflow.com/questions/6198392/check-whether-a-path-is-valid – user469104 Dec 11 '14 at 18:51
  • @user469104 yep, thanks. funny that the error "The path is not of a legal form." ... which is what i searched for ... doesn't appear anywhere in that long established question – hawbsl Dec 11 '14 at 18:53

1 Answers1

0

Old post, but here is a simple try catch solution

    /// <summary>
    /// Return the directory information of the path. Return null if path is not in legal form.
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static string GetDirectoryName(string path)
    {
        try
        {
            return Path.GetDirectoryName(path);
        }
        catch (ArgumentException)
        {
            return null;
        }
    }
Eric Bole-Feysot
  • 13,949
  • 7
  • 47
  • 53