I use the following command for connect to network share:
NET USE \Machine1 /user:MyDomain\MyUser MyPassword
I use C# code programatically (using Process.Start)
ProcessStartInfo psi = new ProcessStartInfo("NET");
string[] userTokens = usuario.Split('\\');
if (userTokens.Length == 2)
{
psi.Arguments = @"USE \\" + maquina + " /user:" + usuario + " " + pwd;
}
else
{
psi.Arguments = @"USE \\" + maquina + " /user:" + maquina + "\\" + usuario + " " + pwd;
}
psi.UseShellExecute = false;
psi.ErrorDialog = false;
psi.RedirectStandardOutput = true;
//psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.CreateNoWindow = true;
using (Process pr = Process.Start(psi))
{
//StreamWriter sw = pr.StandardInput;
//sw.AutoFlush = true;
sr = pr.StandardOutput;
serr = pr.StandardError;
string salida = "";
pr.WaitForExit(300000);
salida += sr.ReadToEnd();
salida += Environment.NewLine;
salida += serr.ReadToEnd();
salida += Environment.NewLine;
Trace.WriteLine("ConectarServidor. NET USE " + maquina + " " + usuario + Environment.NewLine
+ " Salida: " + salida.Trim());
if (salida.Contains("error 1219")
|| salida.Contains("Error de sistema 1219"))
{
// Path is already connected
Trace.WriteLine("Error Net Use 1219: Path is already connected");
TratamientoErrorNetUse1219(maquina, usuario, pwd);
}
else if (salida.Contains("error 86"))
{
//'Incorrect Password
Trace.WriteLine("Error Net Use 86: Incorrect Password");
}
}
Sometimes, there are any errors like this:
Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again.
I want delete the connection (to network share) programatically:
net use (to see all existing connections)
net use * /del /yes (to delete all existing connections)
I try this command but not compatible with net use:
NET USE \Machine1 /del /yes /user:MyDomain\MyUser MyPassword
NET USE * /del /yes /user:MyDomain\MyUser MyPassword
any suggestions about it?