码迷,mamicode.com
首页 > Windows程序 > 详细

c# SSH ,SFTP

时间:2018-11-28 19:08:34      阅读:371      评论:0      收藏:0      [点我收藏+]

标签: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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!