Skip to content

Instantly share code, notes, and snippets.

@Link-God
Last active July 23, 2019 21:10
Show Gist options
  • Save Link-God/7eb4c6e17fa23acb40acdf2abf5ee8a0 to your computer and use it in GitHub Desktop.
Save Link-God/7eb4c6e17fa23acb40acdf2abf5ee8a0 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
using Renci.SshNet;

namespace SSH_1
{
    class Program
    {
        static string GetExtension(string file)
        {
            int LastDotIndex = file.LastIndexOf('.');
            string sub_str = file.Substring(LastDotIndex, file.Length - LastDotIndex);
            return sub_str;
        }
        static void Main(string[] args)
        {
            string host = "127.0.0.1";
            string username = "admin";
            string password = "admin";
            string remoteDirectory = "C:\\Users\\Юрий\\Documents";

            //string host = args[0]; //"192.168.0.1";
            //string username = args[1]; //"admin";
            //string password = args[2]; //"admin";
            //string remoteDirectory = args[3]; // "/xxx/xxxx/xxxxx";

            using (SftpClient sftp = new SftpClient(host, username, password))
            {
                try
                {
                    sftp.Connect();

                    var files = sftp.ListDirectory(remoteDirectory);

                    foreach (var file in files)
                    {
                        if (GetExtension(file.ToString()) == ".docx")
                        {
                            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), file.Name);
                            using (Stream fileStream = File.OpenWrite(path))
                            {
                                sftp.DownloadFile(remoteDirectory + "/" + file.Name, fileStream);
                            }
                        }
                    }

                    sftp.Disconnect();
                }
                catch (Exception e)
                {
                    Console.WriteLine("An exception has been caught " + e.ToString());
                }
            }

        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment