C # application PSFTP implementation SFTP upload

Source: Internet
Author: User
Tags ftp tostring ftp protocol
Upload

SFTP, the first heard, but also thought that the company made a dongdong, Google a bit, the original is an FTP protocol, is a trusted FTP, similar to the HTTPS protocol.

This project is to be some data files packaged through the SFTP to Germany's server, so midway is the need to encrypt transmission, that is, through the SFTP data upload action.

Looking for an open source Dongdong, Psftp, is a green exe file, C # control is also very convenient, so they encapsulate a bit convenient for their applications.

The PSFTP command is a typical UNIX command, easy to understand, and the following are its basic commands:

Using process call in C # to implement FTP upload, the parameters are as follows:

C # is invoked in the following ways:

Upload#region Upload
/**////<summary>
Upload the files
</summary>
<returns>output of the plugin during its running in console</returns>
public string Upload ()
... {
String outputmessage = "";
String scriptlocation = "";

Create Script File
Scriptlocation = this. Createscriptfile ();

Begin for processstartinfo#region begin for ProcessStartInfo
Run the Upload Event
ProcessStartInfo processinfo = new ProcessStartInfo ();
Set the Shell Command (the Plugins ' path)
Processinfo.filename = This.m_shellcommand;
Don ' t show console window
Processinfo.createnowindow = true;
Don ' t use Shell to execute this script
Processinfo.useshellexecute = false;
Open Process Error Output
Processinfo.redirectstandarderror = true;
Open Process Input
Processinfo.redirectstandardinput = true;
Open Process Output
Processinfo.redirectstandardoutput = true;
Get process arguments
string arguments = "";
Arguments + + This.m_userid + "@" + This.m_servername + ""; Login Server with specified userid
Arguments + + "-PW" + This.m_password + ""; Login with specified password
Arguments + = "-P" + This.m_port + ""; Connect to specified port
Arguments + = "B" + scriptlocation + ""; Use specified Batchfile
Arguments + = "-be"; Don ' t stop batchfile processing if errors
Processinfo.arguments = Arguments;
#endregion
Create New Process
Process process = new process ();
Try
... {
Start Execute the ProcessStartInfo
Process. StartInfo = ProcessInfo;
Run the process
Process. Start ();
Input "Y" for the psftp ' s prompt
Just for the security information
Process. Standardinput.writeline ("Y");

Get the "return message" from process (Error and Output information)
This message is logged to the file for debug!
Outputmessage + + process. Standardoutput.readtoend ();
Outputmessage + + process. Standarderror.readtoend ();
Wait for the process exit
Process. WaitForExit ();
Close the modules opened
Process. Close ();
Process. Dispose ();
Delete the script file
File.delete (scriptlocation);
return outputmessage;
}
catch (Exception ex)
... {
Process. Dispose ();
Delete the script file
File.delete (scriptlocation);
throw new Exception ("Error occured during upload file to remote server!", ex);
}
}
#endregion

Createscriptfile#region Createscriptfile
/**////<summary>
Create Batch Script File
</summary>
<returns>file Full path</returns>
private String Createscriptfile ()
... {
StreamWriter FileStream;
String scriptlocation = "";
Get the Batch Script to execute
StringBuilder sbdscript = new StringBuilder ();
Redirect to the default remote location
Sbdscript.append ("CD" + This.m_remotelocation + Environment.NewLine);
Upload files
foreach (object file in This.m_uploadfiles)
... {
Sbdscript.append ("Put" + (string) file + Environment.NewLine);
}

Close the session
Sbdscript.append ("Quit");
Save the Script to Templocation
Scriptlocation = this.m_templocation + @ "" + System.Guid.NewGuid (). ToString () + ". SCR";

Try
... {
FileStream = new StreamWriter (scriptlocation);
FileStream.Write (Sbdscript.tostring ());
Filestream.close ();
}
catch (Exception ex)
... {
FileStream = null;
throw new Exception ("Error occured during Create script file!", ex);
}
return scriptlocation;
}
#endregion


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.