/// <summary> /// Gets a <see cref="SubstitutionDrve"/> for the next available drive letter. /// </summary> /// <param name="targetPath">The path to the folder to map to a drive letter.</param> /// <returns>A <see cref="SubstitutionDrive"/> for the <paramref name="targetPath"/>.</returns> internal static SubstituteDrive Next(string targetPath) { for (var c = 'C'; c <= 'Z'; ++c) { if (!SubstituteDrive.IsDefined(c)) { return(new SubstituteDrive(c, targetPath)); } } throw new InvalidOperationException("No drive letters are available."); }
/// <summary> /// Initializes a new instance of the <see cref="SubstituteDrive"/> class. /// </summary> /// <param name="driveLetter">The drive letter to use for the substitution.</param> /// <param name="targetPath">The path to the folder to map to a drive letter.</param> /// <exception cref="ArgumentException">The <paramref name="driveLetter"/> is already defined.</exception> /// <exception cref="Win32Exception">An error occurred when creating a drive subistution.</exception> internal SubstituteDrive(char driveLetter, string targetPath) { Contract.Requires('C' <= driveLetter && 'Z' >= driveLetter); Contract.Requires(!string.IsNullOrEmpty(targetPath)); driveLetter = char.ToUpperInvariant(driveLetter); if (SubstituteDrive.IsDefined(driveLetter)) { throw new ArgumentException("The drive letter is already defined.", "driveLetter"); } this.DriveLetter = driveLetter + ":"; this.TargetPath = targetPath; if (!SubstituteDrive.DefineDosDevice(DefineDosDeviceFlags.None, this.DriveLetter, this.TargetPath)) { // Throw last error. throw new Win32Exception(); } }