Answers for "SFTP Connect"

C#
0

sftp connect

# Default port is 22
sftp your_user@your_server_ip_or_remote_hostname
# Connexion with a custom port
sftp -oPort=custom_port your_user@your_server_ip_or_remote_hostname
Posted by: Guest on April-24-2021
0

SFTP Connect

using System;
using System.Linq;
using WinSCP;
 
class Program
{
    static int Main(string[] args)
    {
        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Sftp,
                HostName = "example.com",
                UserName = "user",
                Password = "mypassword",
                SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...=",
            };
 
            using (Session session = new Session())
            {
                // Connect
                session.Open(sessionOptions);
 
                const string remotePath = "/home/user";
                const string localPath = @"C:\downloaded";
 
                // Get list of files in the directory
                RemoteDirectoryInfo directoryInfo = session.ListDirectory(remotePath);
 
                // Select the most recent file
                RemoteFileInfo latest =
                    directoryInfo.Files
                        .Where(file => !file.IsDirectory)
                        .OrderByDescending(file => file.LastWriteTime)
                        .FirstOrDefault();
 
                // Any file at all?
                if (latest == null)
                {
                    throw new Exception("No file found");
                }
 
                // Download the selected file
                session.GetFileToDirectory(latest.FullName, localPath);
            }
 
            return 0;
        }
        catch (Exception e)
        {
            Console.WriteLine("Error: {0}", e);
            return 1;
        }
    }
}
Posted by: Guest on July-31-2021

C# Answers by Framework

Browse Popular Code Answers by Language