csharp sftp

Code Example - csharp sftp

                
                        using Renci.SshNet;
using Renci.SshNet.Sftp;
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.Threading.Tasks;
using System.Windows.Forms;
namespace SFtpFileTransfer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var localDir = "D:/temp";
            var remoteDir = "/home/user";

            var files = new List<String>();
            using (SftpClient client = new SftpClient("192.168.1.3", 22, "root", "p@ssw0rd"))
            {
                client.KeepAliveInterval = TimeSpan.FromSeconds(60);
                client.ConnectionInfo.Timeout = TimeSpan.FromMinutes(180);
                client.OperationTimeout = TimeSpan.FromMinutes(180);
                client.Connect();
                bool connected = client.IsConnected;
                client.ChangeDirectory(remoteDir);
                textBox1.Text = client.WorkingDirectory;
                DownloadDirectory(client, remoteDir, localDir );
                client.Disconnect();
                System.Environment.Exit(0);
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            var localDir = "D:/temp";
            var remoteDir = "/home/user";

            var files = new List<String>();
            using (SftpClient client = new SftpClient("192.168.1.3", 22, "root", "p@ssw0rd"))
            {
                client.KeepAliveInterval = TimeSpan.FromSeconds(60);
                client.ConnectionInfo.Timeout = TimeSpan.FromMinutes(180);
                client.OperationTimeout = TimeSpan.FromMinutes(180);
                client.Connect();
                bool connected = client.IsConnected;

                client.ChangeDirectory(remoteDir);
                textBox1.Text = client.WorkingDirectory;
                
                UploadDirectory(client, localDir, remoteDir);

                client.Disconnect();
            }

        }

        public static void DownloadDirectory(SftpClient client, string remotePath, string localPath)
        {
            Directory.CreateDirectory(localPath);
            IEnumerable<SftpFile> files = client.ListDirectory(remotePath);
            foreach (SftpFile file in files)
            {
                if ((file.Name != ".") && (file.Name != ".."))
                {
                    string sourceFilePath = remotePath + "/" + file.Name;
                    string destFilePath = Path.Combine(localPath, file.Name);
                    if (file.IsDirectory)
                    {
                        DownloadDirectory(client, sourceFilePath, destFilePath);
                    }
                    else
                    {
                        using (Stream fileStream = File.Create(destFilePath))
                        {
                            client.DownloadFile(sourceFilePath, fileStream);
                        }
                    }
                }
            }
        }
        public static void UploadDirectory(SftpClient client, string localPath, string remotePath)
        {
            Console.WriteLine("Uploading directory {0} to {1}", localPath, remotePath);

            IEnumerable<FileSystemInfo> infos =
                new DirectoryInfo(localPath).EnumerateFileSystemInfos();
            foreach (FileSystemInfo info in infos)
            {
                if (info.Attributes.HasFlag(FileAttributes.Directory))
                {
                    string subPath = remotePath + "/" + info.Name;
                    if (!client.Exists(subPath))
                    {
                        client.CreateDirectory(subPath);
                    }
                    UploadDirectory(client, info.FullName, remotePath + "/" + info.Name);
                }
                else
                {
                    using (Stream fileStream = new FileStream(info.FullName, FileMode.Open))
                    {
                        Console.WriteLine(
                            "Uploading {0} ({1:N0} bytes)",
                            info.FullName, ((FileInfo)info).Length);

                        client.UploadFile(fileStream, remotePath + "/" + info.Name);
                    }
                }
            }
        }
    }

}