c# SSH ,SFTP

2021-06-28 19:07

阅读:369

标签:names   stat   blog   license   ftpclient   lin   sharp   html   try   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Renci.SshNet;

namespace TestSFTP
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

      
        public class sFtpHelper

        {

            public static int DownloadFtp(string filePath, string localPath, string fileName, string ftpServerIP, string ftpPort, string ftpUserID, string ftpPassword)

            {

                string localFileName = localPath + "\\" + fileName;

                string remoteFileName = filePath+ "/"+ fileName;
                try

                {

                    using (var sftp = new SftpClient(ftpServerIP, Convert.ToInt32(ftpPort), ftpUserID, ftpPassword))

                    {

                        sftp.Connect();



                        using (var file = File.Open(localFileName,FileMode.OpenOrCreate))

                        {
                          
                            sftp.DownloadFile(remoteFileName, file);

                        }



                        sftp.Disconnect();

   
                        Console.WriteLine("下载文件成功,文件路径:"+localFileName);

                        return 0;

                    }

                }

                catch (Exception e)

                {

                  
                    MessageBox.Show("下载失败,原因:"+e.Message );

                    return -2;

                }

            }



            public static int UploadFtp(string filePath, string localPath, string filename, string ftpServerIP, string ftpPort, string ftpUserID, string ftpPassword)

            {

                string localFileName = localPath + "\\" + filename;
                string remoteFileName = "/"+filePath+"/"+filename;

                try

                {

                    using (var sftp = new SftpClient(ftpServerIP, Convert.ToInt32(ftpPort), ftpUserID, ftpPassword))

                    {

                        sftp.Connect();



                        using (var file = File.OpenWrite(localFileName))

                        {

                            sftp.UploadFile(file, remoteFileName);

                        }



                        sftp.Disconnect();

                        Console.WriteLine("上传文件成功,文件路径:"+localFileName);

                        return 0;

                    }

                }

                catch (Exception ex)

                {

                  
                    Console.WriteLine("上传失败,原因:"+ex.Message );

                    return -2;

                }

            }



        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sftpHost = "sftp.xxx.com";
            string sFile= "aaa.txt";
            sFtpHelper.DownloadFtp("/upload/weblicense/dd", "d:\\",sFile , sftpHost, "22", "username222", "pwd123");




        }

}

}

  

SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism.

【从nuget中下载SSH.NET】

摘自 http://www.cnblogs.com/cbread/p/6202069.html

 

c# SSH ,SFTP

标签:names   stat   blog   license   ftpclient   lin   sharp   html   try   

原文地址:https://www.cnblogs.com/wgscd/p/10033626.html


评论


亲,登录后才可以留言!