Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# 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);
                    }
                }
            }
        }
    }

}
Comment

PREVIOUS NEXT
Code Example
Csharp :: sum of digit of number c# 
Csharp :: get ad user using email address microsoft graph C# 
Csharp :: c# lambdas 
Csharp :: unity camera fade to black 
Csharp :: c# get all letters 
Csharp :: distance between two objects unity 2d 
Csharp :: delete all rows from table mvc 
Csharp :: how to redirect to another page in button clicked in asp.net c# index.cshtml 
Csharp :: c# tuple 
Csharp :: exceeds your upload_max_filesize ini directive (limit is 2048 KiB). 
Csharp :: C# get column of 2d array 
Csharp :: c# shorthand if statement without else 
Csharp :: count the number of notes in a given amount c# 
Csharp :: render section asp.net mvc layout 
Csharp :: how to get the today date in c# 
Csharp :: c# check characters in string 
Csharp :: open linkedlabel c# 
Csharp :: listview thread error 
Csharp :: sucess messages in c# 
Csharp :: c# custom event handler with parameters 
Csharp :: how set format persian data picker to en 
Csharp :: how to detect ajax request in asp.net core 
Csharp :: remove string inside curly braces C# 
Csharp :: Getting the text from a drop-down box 
Csharp :: asp.net c# get user email address from AD 
Csharp :: string is int f# 
Csharp :: how to edit .csproj file 
Csharp :: indexing an array 
Csharp :: c# httpclient post no content 
Csharp :: how to instantiate and delete unity 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =