Created
November 24, 2017 23:45
-
-
Save DCCoder90/7f30f74ff59df4a2d4b0143d2fcfe2cc to your computer and use it in GitHub Desktop.
Part of a larger project. This section temporarily maps a network drive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Imager : IDisposable{ | |
private string _persist = "NO"; | |
private Process _networkMap; | |
public void Dispose() | |
{ | |
_networkMap.Close(); | |
} | |
private void MapDrive(){ | |
_networkMap = new System.Diagnostics.Process(); | |
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(); | |
psi.FileName = "cmd.exe"; | |
psi.Arguments = @"/C net use X: \\SERVER\FOLDER /PERSISTENT:"+_persist; | |
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; | |
psi.UseShellExecute = true; | |
_networkMap.StartInfo = psi; | |
try{ | |
_networkMap.Start(); | |
}catch(Win32Exception ex){ | |
ShowAdminError(ex); | |
} | |
} | |
private void ShowAdminError(Win32Exception ex){ | |
if (ex.NativeErrorCode == 1223) | |
MessageBox.Show("Program must be run as administrator!"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment