public static string GetDirectoryName (string path)
{
// LAMESPEC: For empty string MS docs say both
// return null AND throw exception. Seems .NET throws.
if (path == String.Empty)
throw new ArgumentException("Invalid path");
if (path == null || GetPathRoot (path) == path)
return null;
if (path.Trim ().Length == 0)
throw new ArgumentException ("Argument string consists of whitespace characters only.");
if (path.IndexOfAny (System.IO.Path.InvalidPathChars) > -1)
throw new ArgumentException ("Path contains invalid characters");
int nLast = path.LastIndexOfAny (PathSeparatorChars);
if (nLast == 0)
nLast++;
if (nLast > 0) {
string ret = path.Substring (0, nLast);
int l = ret.Length;
if (l >= 2 && DirectorySeparatorChar == '\\' && ret [l - 1] == VolumeSeparatorChar)
return ret + DirectorySeparatorChar;
else {
//
// Important: do not use CanonicalizePath here, use
// the custom CleanPath here, as this should not
// return absolute paths
//
return CleanPath (ret);
}
}
return String.Empty;
}