Thursday, November 19, 2015

How to map network drives programmatically in C#


Follow the next example to map a network drive programmatically:

// Map Network drive
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();

// Notes:
//      Use /C To carry out the command specified by string and then terminates
//      You can omit the passord or username and password
//      Use /PERSISTENT:YES to keep the mapping when the machine is restarted
psi.FileName      = "cmd.exe";
psi.Arguments     = @"/C net use X: \\MyServer\Folder01  /USER:MyDomain\MyUsername MyPassword /PERSISTENT:YES";
psi.WindowStyle   = System.Diagnostics.ProcessWindowStyle.Normal;
process.StartInfo = psi;

process.Start();


1 comment:

  1. if it is disconnected it is not connecting

    ReplyDelete