Use the net use command to copy files on a remote machine.
For example, to copy the file of machine B to machine A through machine A, you need to add the user permission of machine A to the machine B folder.
Public static bool Ping (string remotehost)
{
Bool flag = false;
Process proc = new process ();
Try
{
Proc. startinfo. filename = "cmd.exe ";
Proc. startinfo. useshellexecute = false;
Proc. startinfo. redirectstandardinput = true;
Proc. startinfo. redirectstandardoutput = true;
Proc. startinfo. redirectstandarderror = true;
Proc. startinfo. createnowindow = true;
Proc. Start ();
String dosline = @ "Ping-N 1" + remotehost;
Proc. standardinput. writeline (dosline );
Proc. standardinput. writeline ("exit ");
While (Proc. hasexited = false)
{
Proc. waitforexit (500 );
}
String pingresult = Proc. standardoutput. readtoend ();
If (pingresult. indexof ("(0% loss )")! =-1)
{
Flag = true;
}
Proc. standardoutput. Close ();
}
Catch (exception ex)
{
}
Finally
{
Try
{
Proc. Close ();
Proc. Dispose ();
}
Catch
{
}
}
Return flag;
}
Public static bool connect (string remotehost, string username, string password)
{
If (! Ping (remotehost ))
{
Return false;
}
Bool flag = true;
Process proc = new process ();
Try
{
Proc. startinfo. filename = "cmd.exe ";
Proc. startinfo. useshellexecute = false;
Proc. startinfo. redirectstandardinput = true;
Proc. startinfo. redirectstandardoutput = true;
Proc. startinfo. redirectstandarderror = true;
Proc. startinfo. createnowindow = true;
Proc. Start ();
String dosline = @ "net use \" + remotehost + "" + password + "" + "/User:" + username + "> NUL ";
Proc. standardinput. writeline (dosline );
Proc. standardinput. writeline ("exit ");
While (Proc. hasexited = false)
{
Proc. waitforexit (1000 );
}
String errormsg = Proc. standarderror. readtoend ();
If (errormsg! = "")
{
Flag = false;
}
Proc. standarderror. Close ();
}
Catch (exception ex)
{
Flag = false;
}
Finally
{
Try
{
Proc. Close ();
Proc. Dispose ();
}
Catch
{
}
}
Return flag;
}
The two methods are to establish a connection to machine B. After the connection is successful, use file. Copy.
If (Ping ("machine B "))
{
If (connect ("machine B", "Machine a user", "Machine a user access password "))
{
File. Copy ("\ MACHINE B \ file path \ file name", "Machine A's complete file storage path", true );
}
}