/// <summary> /// Creates a new mapping /// </summary> /// <param name="path">The path to map</param> /// <param name="drive">The drive to map to, use null to get a free drive letter</param> /// <param name="notifyShell">True to notify the shell of the change, false otherwise</param> public DefineDosDevice(string path, string drive, bool notifyShell) { if (string.IsNullOrEmpty(drive)) { List <char> drives = new List <char>("DEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()); foreach (DriveInfo di in DriveInfo.GetDrives()) { if ((di.RootDirectory.FullName.Length == 2 && di.RootDirectory.FullName[1] == ':') || ((di.RootDirectory.FullName.Length == 3 && di.RootDirectory.FullName.EndsWith(":\\")))) { int i = drives.IndexOf(di.RootDirectory.FullName[0]); if (i >= 0) { drives.RemoveAt(i); } } } if (drives.Count == 0) { throw new IOException("No drive letters available"); } drive = drives[0].ToString() + ':'; } while (drive.EndsWith("\\")) { drive = drive.Substring(0, drive.Length - 1); } if (!drive.EndsWith(":")) { throw new ArgumentException("The drive specification must end with a colon.", nameof(drive)); } Win32API.DDD_Flags flags = 0; if (!notifyShell) { flags |= Win32API.DDD_Flags.DDD_NO_BROADCAST_SYSTEM; } if (!Win32API.DefineDosDevice(flags, drive, path)) { throw new System.ComponentModel.Win32Exception(); } m_drive = drive; m_targetPath = path; m_shellBroadcast = notifyShell; }
/// <summary> /// Disposes all resources /// </summary> /// <param name="disposing">True if called from Disposing, false otherwise</param> protected void Dispose(bool disposing) { if (m_drive != null) { Win32API.DDD_Flags flags = Win32API.DDD_Flags.DDD_REMOVE_DEFINITION | Win32API.DDD_Flags.DDD_EXACT_MATCH_ON_REMOVE; if (m_shellBroadcast) { flags |= Win32API.DDD_Flags.DDD_NO_BROADCAST_SYSTEM; } Win32API.DefineDosDevice(flags, m_drive, m_targetPath); m_drive = null; } if (disposing) { GC.SuppressFinalize(this); } }