상속: IDisposable
예제 #1
0
        private static bool IsDefined(char driveLetter)
        {
            var drive  = driveLetter + ":";
            var target = new StringBuilder(SubstituteDrive.MAX_PATH);

            return(0 != SubstituteDrive.QueryDosDevice(drive, target, target.Capacity));
        }
예제 #2
0
        /// <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.");
        }
예제 #3
0
        /// <summary>
        /// Remove the drive substitution.
        /// </summary>
        public void Dispose()
        {
            if (null != this.DriveLetter)
            {
                SubstituteDrive.DefineDosDevice(DefineDosDeviceFlags.RemoveExactMatch, this.DriveLetter, this.TargetPath);

                this.DriveLetter = null;
                this.TargetPath  = null;

                GC.SuppressFinalize(this);
            }
        }
예제 #4
0
        /// <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();
            }
        }