Skip to content

Instantly share code, notes, and snippets.

@DCCoder90
Created November 24, 2017 23:45
Show Gist options
  • Save DCCoder90/7f30f74ff59df4a2d4b0143d2fcfe2cc to your computer and use it in GitHub Desktop.
Save DCCoder90/7f30f74ff59df4a2d4b0143d2fcfe2cc to your computer and use it in GitHub Desktop.
Part of a larger project. This section temporarily maps a network drive.
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