public static string GetDirectoryName(string path) { if (Common.IsRunningOnMono()) { return(System.IO.Path.GetDirectoryName(path)); } if (path == null) { throw new ArgumentNullException("path"); } Path.CheckInvalidPathChars(path); string basePath = null; if (!IsPathRooted(path)) { basePath = System.IO.Directory.GetCurrentDirectory(); } path = Path.RemoveLongPathPrefix(Path.NormalizeLongPath(path)); int rootLength = GetRootLength(path); if (path.Length <= rootLength) { return(null); } int length = path.Length; do { } while (length > rootLength && path[--length] != System.IO.Path.DirectorySeparatorChar && path[length] != System.IO.Path.AltDirectorySeparatorChar); if (basePath != null) { path = path.Substring(basePath.Length + 1); length = length - basePath.Length - 1; if (length < 0) { length = 0; } } return(path.Substring(0, length)); }
// $$modif pb public static int GetRootLength(string path) { if (Common.IsPathUnc(path)) { return(GetUncRootLength(path)); } // $$modif pb //path = Path.GetFullPath(path); Path.CheckInvalidPathChars(path); int rootLength = 0; int length = path.Length; if (length >= 1 && IsDirectorySeparator(path[0])) { rootLength = 1; if (length >= 2 && IsDirectorySeparator(path[1])) { rootLength = 2; int num = 2; while (rootLength >= length || ((path[rootLength] == System.IO.Path.DirectorySeparatorChar || path[rootLength] == System.IO.Path.AltDirectorySeparatorChar) && --num <= 0)) { ++rootLength; } } } else if (length >= 2 && path[1] == System.IO.Path.VolumeSeparatorChar) { rootLength = 2; if (length >= 3 && IsDirectorySeparator(path[2])) { ++rootLength; } } return(rootLength); }