C # copying files from a remote machine

Source: Internet
Author: User

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 );
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.