Using system;using system.collections;using system.collections.generic;using system.io;using Tamir.SharpSsh.java.io ; using Tamir.sharpssh.jsch;namespace EB. mall.core.utils{public class Sftphelper {private Session m_session; Private Channel M_channel; Private Channelsftp m_sftp; HOST:SFTP Address User: Username pwd: Password public sftphelper (string host, String user, string pwd) { String[] arr = host. Split (': '); string ip = arr[0]; int port = 22; if (arr. Length > 1) port = Int32.Parse (arr[1]); Jsch Jsch = new Jsch (); M_session = jsch.getsession (user, IP, port); Myuserinfo UI = new Myuserinfo (); Ui.setpassword (PWD); M_session.setuserinfo (UI); }//SFTP connection state public bool Connected {get {return m_session.isconnected ()}} Connect SFTP public bool Connect () {try { if (! Connected) {m_session.connect (); M_channel = M_session.openchannel ("sftp"); M_channel.connect (); M_sftp = (channelsftp) M_channel; } return true; } catch (Exception ex) {return false; }}//disconnect sftp public void Disconnect () {if (Connected) { M_channel.disconnect (); M_session.disconnect (); }}//sftp holds file public bool Put (string LocalPath, String remotepath) {try {Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String (LocalPath); Tamir.SharpSsh.java.String DST = new Tamir.SharpSsh.java.String (RemotePath); M_sftp.put (SRC, DST); return true; } CAtch {return false; }}//sftp Store file public bool Put (byte[] filecontent, string remotepath) {T ry {Stream memstream = new MemoryStream (filecontent); Tamir.SharpSsh.java.String DST = new Tamir.SharpSsh.java.String (RemotePath); Putstream (Memstream, RemotePath); return true; } catch (Exception ex) {return false; }} public bool Putstream (System.IO.Stream LocalFile, string remotepath) {try {Tamir.SharpSsh.java.String DST = new Tamir.SharpSsh.java.String (RemotePath); M_sftp.put (New Tamir.Streams.InputStreamWrapper (LocalFile), DST, 1); Localfile.dispose (); return true; } catch (Exception ex) {throw ex; } } public bool MkDir (string path) {try {m_sftp.mkdir (path); return true; } catch (Exception ex) {throw ex; }}//sftp get file public bool Get (string RemotePath, String localPath) {try {Tamir.SharpSsh.java.String src = new Tamir.SharpSsh.java.String (RemotePath); Tamir.SharpSsh.java.String DST = new Tamir.SharpSsh.java.String (LocalPath); M_sftp.get (SRC, DST); return true; } catch (Exception ex) {return false; }}//delete sftp file public bool Delete (string remotefile) {try { M_SFTP.RM (RemoteFile); return true; } catch {return false; }}//Get SFTP textItem List Public ArrayList getfilelist (string remotepath, String fileType) {try { Tamir.SharpSsh.java.util.Vector VVV = M_sftp.ls (RemotePath); ArrayList objlist = new ArrayList (); foreach (Tamir.SharpSsh.jsch.ChannelSftp.LsEntry qqq in VVV) {string sss = Qqq.getfilen Ame (); if (SSS. Length > (filetype.length + 1) && FileType = sss. Substring (SSS. Length-filetype.length)) {Objlist.add (SSS);} else {continue;} } return objlist; } catch {return null; }}//Login authentication Information public class Myuserinfo:userinfo {String passwd; Public String GetPassword () {return passwd;} public void SetPassword (String passwd) {this.passwd = passwd;} Public STring Getpassphrase () {return null;} public bool Promptpassphrase (String message) {return true;} public bool Promptpassword (String message) {return true;} public bool Promptyesno (String message) {return true;} public void ShowMessage (String message) {}}}}
sftp upload download file, part of the code taken from the network, you need to add DiffieHellman.dll, Org.Mentalis.Security.dll, Tamir.SharpSSH.dll three DLL references