public static EndWithDirectorySeparator ( string path ) : string | ||
path | string | The path information to modify. |
Résultat | string |
/// <summary> /// Changes the parent of a <paramref name="path"/> string. /// </summary> /// <param name="path">The path information to modify.</param> /// <param name="oldParent">The old parent.</param> /// <param name="newParent">The new parent.</param> /// <returns>The modified path information.</returns> /// <exception cref="ArgumentNullException">Any of the parameters is <see langword="null"/> or empty.</exception> /// <exception cref="ArgumentException"><paramref name="path"/> string does not start with the <paramref name="oldParent"/>.</exception> /// <remarks> /// This method is useful, when you need not just replace the path root, but one of its parent.<br/> /// For example, to transform <c>C:\Source\SubDir\File.txt</c> into <c>D:\Target\SubDir\File.txt</c> the following code may be used: /// <code> /// string targetFile = PathHelper.ChangeParent(@"C:\Source\File.txt", @"C:\Source", @"D:\Target"); /// </code> /// </remarks> public static string ChangeParent(string path, string oldParent, string newParent) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException(nameof(path)); } if (string.IsNullOrEmpty(oldParent)) { throw new ArgumentNullException(nameof(oldParent)); } if (newParent == null) { throw new ArgumentNullException(nameof(newParent)); } oldParent = PathHelper.EndWithDirectorySeparator(oldParent); newParent = string.IsNullOrEmpty(newParent) ? string.Empty : PathHelper.EndWithDirectorySeparator(newParent); if (!path.StartsWith(oldParent, StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException(string.Format(CultureInfo.InvariantCulture, "Path '{0}' does not start with '{1}'", path, oldParent)); } return(newParent + path.Substring(oldParent.Length)); }
/// <summary> /// Updates the internal maps between the volume names (<c>D:\</c>) and device names (<c>\Device\HarddiskVolume2\</c>). /// </summary> private static void UpdateDeviceMappings() { List <string> driveLetters = DriveInfo.GetDrives().Select(d => d.RootDirectory.Name).Select(PathHelper.EndWithDirectorySeparator).ToList(); foreach (string driveLetter in driveLetters) { string deviceName = PathHelper.EndWithDirectorySeparator(PathHelper.GetDeviceDosName(driveLetter)); PathHelper.DriveLetterToDeviceName[driveLetter] = deviceName; } }