/// <summary> /// Map network drive (using supplied Username and Password) /// </summary> public IntPtr MapDrive(string UserDomain, string Username, string Password, string shareName) { string driveLetter = getFirstUnUsedDriveLetter(); if (driveLetter == String.Empty) { throw new Exception("No drive letter."); } this.LocalDrive = driveLetter; this.ShareName = shareName; IntPtr impersonationHandle = UserImpersonation.StartImpersonating(UserDomain, Username, Password); zMapDrive(getUsername(UserDomain, Username), Password); return(impersonationHandle); }
/// <summary> /// Unmap network drive /// </summary> public void UnMapDrive(IntPtr impersonationHandle) { try { zUnMapDrive(this.lf_Force); } catch (Exception ex) { Console.WriteLine("Cannot disconnect map drive: " + ex.Message); } try { UserImpersonation.StopImpersonating(impersonationHandle); } catch (Exception ex) { Console.WriteLine("Cannot stop impersonating: " + ex.Message); } }