Because the service provider migrates from the FTP server to the SFTP server, alas ~ ~ ~, the bitter force we will change to the right. Now let's talk a little bit about how this is achieved.
----------------------------------------I'm a shameless split-line------------------------------------------------------------
Do not repeat the difference between FTP and SFTP, only to mention a little to a certain extent the latter more secure, the need for students to visit http://www.cnblogs.com/mfryf/archive/2013/05/29/3107286.html to learn more, thank you!
Well, since we have to go to the server to operate the file, we first have to log on to the server, just like other servers, we need an address, user name, password, may also need a port number.
Here I am with the help of a third-party plugin, Sharpssh 1.1.1.13, which is the address http://www.tamirgal.com/blog/page/SharpSSH.aspx (you can download it yourself).
--DLLs that need to be referenced
Using Tamir.sharpssh;
Using AMAZONASNSERVICE.BLL;
classSftphelp {Privatesshtransferprotocolbase m_sshcp; Privatesftphelp () {} Publicsftphelp (Sshconnectioninfo connectioninfo) {m_sshcp=NewSftp (Connectioninfo.host, Connectioninfo.user); if(Connectioninfo.pass! =NULL) {M_sshcp.password=Connectioninfo.pass; } if(Connectioninfo.identityfile! =NULL) {m_sshcp.addidentityfile (connectioninfo.identityfile); } } Public BOOLConnected {Get { returnm_sshcp.connected; } } Public voidConnect () {if(!m_sshcp.connected) {M_sshcp.connect (); } } Public voidClose () {if(m_sshcp.connected) {m_sshcp.close (); } } //Upload Public BOOLUpload (stringLocalPath,stringRemotePath,intPort) { Try { if(!m_sshcp.connected) {M_sshcp.connect (port);//If there is no port number, you can ignore} m_sshcp.put (LocalPath, Remotepa TH); return true; } Catch { return false; } } //Download Public BOOLDownload (stringRemotePath,stringLocalPath,intPort) { Try { if(!m_sshcp.connected) {M_sshcp.connect (port); } m_sshcp.get (RemotePath, LocalPath); return true; } Catch { return false; } }
Public BOOLDelete (stringRemotePath) { Try { if(!m_sshcp.connected) {M_sshcp.connect (); } ((SFTP) m_sshcp). Delete (RemotePath);//the newly added delete method return true; } Catch { return false; } } PublicArrayList Getfilelist (stringPathintPort) { Try { if(!m_sshcp.connected) {M_sshcp.connect (port); } return(SFTP) (M_SSHCP). Getfilelist (path); } Catch(Exception ex) {return NULL; } } }
/// <summary> ///sftp Uploading Files/// </summary> Private Static voiduploadsftp () {sshconnectioninfo objinfo=NewSshconnectioninfo (); Objinfo.user=Ftpuserid; Objinfo.host=Ftpserverip; Objinfo.pass= FTPPassword;//based on passwordSftphelp Objsftphelper =Newsftphelp (Objinfo); string[] Strfiles =Directory.GetFiles (Poapath); if(Strfiles! =NULL) { foreach(stringStrfileinchstrfiles) { if(Objsftphelper.upload (strfile, FTPDir,2222))//Start uploading{file.delete (strfile); } } } }
View Code
/// <summary> ///2. Get files from SFTP to the specified directory/// </summary> Private Static voidDownsftpfile () {sshconnectioninfo ssinfo=NewSshconnectioninfo (); Ssinfo.user=Ftpuserid; Ssinfo.host=Ftpserverip; //ssinfo.identityfile = "key";//There are 2 certifications, one based on Privatekey, and one based on passwordSsinfo.pass = FTPPassword;//based on passwordSftphelp Sftpdown =Newsftphelp (Ssinfo); ArrayList FileList= Sftpdown.getfilelist (Ftppath,2222); if(FileList! =NULL) { for(inti =0; i < Filelist.count; i++) { stringsFileName = Ftppath +"/"+Filelist[i]; if(Sftpdown.download (sFileName, Downpath,2222) {sftpdown.delete (sFileName,2222); } } } }
View Code
. NET operations on files on an SFTP server