/// <summary> /// Unmaps a drive /// </summary> /// <param name="d">Drive object to unmap</param> /// <returns></returns> public static int Unmap(NetworkDrive d) { uint result = WNetCancelConnection2(d.DriveLetter, CONNECT_UPDATE_PROFILE, true); Logger.Log("\t\tUnmap " + d.DriveLetter + "; result: " + TransResult(result)); return((int)result); }
/// <summary> /// Maps a network drive /// </summary> /// <param name="d">Drive Letter to map, e.g D:</param> /// <param name="path">Full UNC path, e.g. \\server\path\folder</param> /// <returns></returns> public static int Map(NetworkDrive d, string path) { NETRESOURCE networkResource = new NETRESOURCE { dwType = RESOURCETYPE_DISK, lpLocalName = d.DriveLetter, lpRemoteName = path, lpProvider = null }; uint result = WNetAddConnection2(ref networkResource, null, null, CONNECT_UPDATE_PROFILE); Logger.Log("\t\tMap " + d.DriveLetter + " to " + path + "; result: " + TransResult(result)); return((int)result); }
/// <summary> /// Remaps a drive by first unmapping the old drive and then mapping the new path. /// </summary> /// <param name="d">Drive letter to map, e.g. D:</param> /// <param name="s">Full UNC path to map, e.g. \\server\path\folder</param> public static void Remap(NetworkDrive d, string s) { Logger.Log("\t\tAttempting to remap " + d.DriveLetter + "..."); if (Unmap(d) == 0) { if (Map(d, s) != 0) { Logger.Log("\t\tMapping failed; reverting to previous mapping..."); Map(d, d.OriginalFullPath); } } else { Logger.Log("\t\tUnmapping failed."); } }