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(); } if (path == null || GetPathRoot(path) == path) { return(null); } CheckArgument.WhitespaceOnly(path); CheckArgument.PathChars(path); 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 && ret[l - 1] == VolumeSeparatorChar) { return(ret + DirectorySeparatorChar); } else { return(ret); } } return(String.Empty); }