C # Operations SFTP Summary

Source: Internet
Author: User

Recent projects need to use SFTP to upload, download files, after looking at the data, the POC finally repaired to fruition, the specific implementation process will be written down to facilitate the same needs of brothers, sisters reference.

Name Explanation

SFTP: Securefiletransferprotocol, security ftp. There is an encryption process relative to FTP.

Personally summed up a bit to achieve C # SFTP operations (upload, download, delete, traverse, etc.) there are 2 faster ways

1. Purchase of a third party class library

2. Use open source class library (more popular: sharpssh,sshnet)

Here's the sharpssh. The following is based on Sharpssh.

Specific Steps

1. Download Sharpssh source code from the website (http://www.tamirgal.com/blog/page/SharpSSH.aspx)

Here you can download the source +bin file, note: If only download bin file, inside the class library is not include delete file operation.

Because of the project requirements, we need to delete the remote files, so download the source code, add the delete Function, and then recompile

SHARPSSH Source Structure


We need to include the delete function in the Sftp.cs file, as shown in the following figure.


Recompile, and then reference the generated 3 DLLs to the project



2. Based on Sharpssh, realize the common operation of SFTP

Create a new SFTPHelper.cs

public class Sftphelper {private Sshtransferprotocolbase m_sshcp;
            Private Sftphelper () {} public sftphelper (Sshconnectioninfo connectioninfo) {

            M_SSHCP = new Sftp (Connectioninfo.host, Connectioninfo.user);
            if (Connectioninfo.pass!= null) {M_sshcp.password = Connectioninfo.pass; } if (Connectioninfo.identityfile!= null) {M_sshcp.addidentityfile (connectionin Fo.
            Identityfile); } public bool Connected {get {return m_sshcp.connecte
            D } public void Connect () {if (!m_sshcp.connected) {M_SS
            Hcp.connect (); } public void Close () {if (m_sshcp.connected) {M_SSHCP .
            Close ();
     }
        }   public bool Upload (string localpath, String remotepath) {try {if (!m_
                sshcp.connected) {m_sshcp.connect ();

                } m_sshcp.put (LocalPath, RemotePath);
            return true;
            catch {return false;
                public bool Download (string RemotePath, String localpath) {try {
                if (!m_sshcp.connected) {m_sshcp.connect ();

                } m_sshcp.get (RemotePath, LocalPath);
            return true;
            catch {return false; public bool Delete (string remotepath) {try {if!m_s
                shcp.connected) {m_sshcp.connect ();} ((SFTP) m_sshcp).
            Delete (RemotePath);//Just new Delete method return true;
            catch {return false; } Public ArrayList Getfilelist (string path) {try {if
                (!m_sshcp.connected)
                {M_sshcp.connect (); Return ((SFTP) m_sshcp).
            Getfilelist (path);
            catch {return null; }
        }
    }
Create a secondary class

public class Sshconnectioninfo
    {public
        string Identityfile {get; set;}
        public string Pass {get; set;}
        public string Host {get; set;}
        public string User {get; set;}
    }

Uploading files

Sshconnectioninfo objinfo = new Sshconnectioninfo ();
            Objinfo.user = "username";
            Objinfo.host = "Host";
            Objinfo.identityfile = "key"; There are 2 certifications, one based on Privatekey, one based on password
            //objinfo.pass = "password"; Based on password
            sftphelper objsftphelper = new Sftphelper (objinfo);
            Objsftphelper.upload ("LocalFile", "remotefile");


Download files

Objsftphelper.download ("RemoteFile", "LocalFile");

deleting files

Objsftphelper.delete ("RemoteFile");

Traverse Remote Folder

ArrayList filelist = objsftphelper.getfilelist ("RemotePath");


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.