/// <summary>Gets the root directory information of the specified path.</summary> /// <returns>A string containing the root directory of <paramref name="path" />, such as "C:\", or null if <paramref name="path" /> is null, or an empty string if <paramref name="path" /> does not contain root directory information.</returns> /// <param name="path">The path from which to obtain root directory information. </param> /// <exception cref="T:System.ArgumentException"> /// <paramref name="path" /> contains one or more of the invalid characters defined in <see cref="M:System.IO.Path.GetInvalidPathChars" />.-or- <see cref="F:System.String.Empty" /> was passed to <paramref name="path" />. </exception> /// <filterpriority>1</filterpriority> public static string GetPathRoot(string path) { if (path == null) { return(null); } if (path.Trim().Length == 0) { throw new ArgumentException("The specified path is not of a legal form."); } if (!Path.IsPathRooted(path)) { return(string.Empty); } if (Path.DirectorySeparatorChar == '/') { return((!Path.IsDsc(path[0])) ? string.Empty : Path.DirectorySeparatorStr); } int num = 2; if (path.Length == 1 && Path.IsDsc(path[0])) { return(Path.DirectorySeparatorStr); } if (path.Length < 2) { return(string.Empty); } if (Path.IsDsc(path[0]) && Path.IsDsc(path[1])) { while (num < path.Length && !Path.IsDsc(path[num])) { num++; } if (num < path.Length) { num++; while (num < path.Length && !Path.IsDsc(path[num])) { num++; } } return(Path.DirectorySeparatorStr + Path.DirectorySeparatorStr + path.Substring(2, num - 2).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)); } if (Path.IsDsc(path[0])) { return(Path.DirectorySeparatorStr); } if (path[1] == Path.VolumeSeparatorChar) { if (path.Length >= 3 && Path.IsDsc(path[2])) { num++; } return(path.Substring(0, num)); } return(Directory.GetCurrentDirectory().Substring(0, 2)); }
private static string GetServerAndShare(string path) { int num = 2; while (num < path.Length && !Path.IsDsc(path[num])) { num++; } if (num < path.Length) { num++; while (num < path.Length && !Path.IsDsc(path[num])) { num++; } } return(path.Substring(2, num - 2).Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar)); }
private static bool SameRoot(string root, string path) { if (root.Length < 2 || path.Length < 2) { return(false); } if (!Path.IsDsc(root[0]) || !Path.IsDsc(root[1])) { return(root[0].Equals(path[0]) && path[1] == Path.VolumeSeparatorChar && (root.Length <= 2 || path.Length <= 2 || (Path.IsDsc(root[2]) && Path.IsDsc(path[2])))); } if (!Path.IsDsc(path[0]) || !Path.IsDsc(path[1])) { return(false); } string serverAndShare = Path.GetServerAndShare(root); string serverAndShare2 = Path.GetServerAndShare(path); return(string.Compare(serverAndShare, serverAndShare2, true, CultureInfo.InvariantCulture) == 0); }
private static string CanonicalizePath(string path) { if (path == null) { return(path); } if (Environment.IsRunningOnWindows) { path = path.Trim(); } if (path.Length == 0) { return(path); } string pathRoot = Path.GetPathRoot(path); string[] array = path.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); int num = 0; bool flag = Environment.IsRunningOnWindows && pathRoot.Length > 2 && Path.IsDsc(pathRoot[0]) && Path.IsDsc(pathRoot[1]); int num2 = (!flag) ? 0 : 3; for (int i = 0; i < array.Length; i++) { if (Environment.IsRunningOnWindows) { array[i] = array[i].TrimEnd(new char[0]); } if (!(array[i] == ".") && (i == 0 || array[i].Length != 0)) { if (array[i] == "..") { if (num > num2) { num--; } } else { array[num++] = array[i]; } } } if (num == 0 || (num == 1 && array[0] == string.Empty)) { return(pathRoot); } string text = string.Join(Path.DirectorySeparatorStr, array, 0, num); if (!Environment.IsRunningOnWindows) { return(text); } if (flag) { text = Path.DirectorySeparatorStr + text; } if (!Path.SameRoot(pathRoot, text)) { text = pathRoot + text; } if (flag) { return(text); } if (!Path.IsDsc(path[0]) && Path.SameRoot(pathRoot, path)) { if (text.Length <= 2 && !text.EndsWith(Path.DirectorySeparatorStr)) { text += Path.DirectorySeparatorChar; } return(text); } string currentDirectory = Directory.GetCurrentDirectory(); if (currentDirectory.Length > 1 && currentDirectory[1] == Path.VolumeSeparatorChar) { if (text.Length == 0 || Path.IsDsc(text[0])) { text += '\\'; } return(currentDirectory.Substring(0, 2) + text); } if (Path.IsDsc(currentDirectory[currentDirectory.Length - 1]) && Path.IsDsc(text[0])) { return(currentDirectory + text.Substring(1)); } return(currentDirectory + text); }
internal static string InsecureGetFullPath(string path) { if (path == null) { throw new ArgumentNullException("path"); } if (path.Trim().Length == 0) { string text = Locale.GetText("The specified path is not of a legal form (empty)."); throw new ArgumentException(text); } if (Environment.IsRunningOnWindows) { path = Path.WindowsDriveAdjustment(path); } char c = path[path.Length - 1]; if (path.Length >= 2 && Path.IsDsc(path[0]) && Path.IsDsc(path[1])) { if (path.Length == 2 || path.IndexOf(path[0], 2) < 0) { throw new ArgumentException("UNC paths should be of the form \\\\server\\share."); } if (path[0] != Path.DirectorySeparatorChar) { path = path.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar); } path = Path.CanonicalizePath(path); } else { if (!Path.IsPathRooted(path)) { path = Directory.GetCurrentDirectory() + Path.DirectorySeparatorStr + path; } else if (Path.DirectorySeparatorChar == '\\' && path.Length >= 2 && Path.IsDsc(path[0]) && !Path.IsDsc(path[1])) { string currentDirectory = Directory.GetCurrentDirectory(); if (currentDirectory[1] == Path.VolumeSeparatorChar) { path = currentDirectory.Substring(0, 2) + path; } else { path = currentDirectory.Substring(0, currentDirectory.IndexOf('\\', currentDirectory.IndexOf("\\\\") + 1)); } } path = Path.CanonicalizePath(path); } if (Path.IsDsc(c) && path[path.Length - 1] != Path.DirectorySeparatorChar) { path += Path.DirectorySeparatorChar; } string text2; if (MonoIO.RemapPath(path, out text2)) { path = text2; } return(path); }