简体   繁体   中英

How to download and upload file using SshClient .net

I get Renci.SshNet.Common.SshOperationTimeoutException when using SftpClient, currently i only able to use SshClient, is there a way to download and upload file using SshClient? Could any one shed some light on this?

I tried ftp 10.101.10.10 in window command prompt, it works flawlessly. File is 1KB only.

//this code gives Renci.SshNet.Common.SshOperationTimeoutException
using (var sftp = new SftpClient("10.101.10.10", "user", "passcode"))
{
    sftp.Connect(); 
    sftp.Timeout = new TimeSpan(0, 0, 60);
    sftp.Disconnect()
}


//This code work!
using (var client = new SshClient("10.101.10.10", "user", "passcode"))
{
    client.Connect();
    //i did tried with some other list file command here, it work also, but i don't know how to download and upload file
    client.Disconnect();
}

Not sure why, but i did tried WinSCP, and it working.

        try
        {
            // Setup session options
            SessionOptions sessionOptions = new SessionOptions
            {
                Protocol = Protocol.Ftp,
                FtpSecure = FtpSecure.None,
                HostName = "10.101.10.10",
                UserName = "user",
                PortNumber = 21,
                Password = "passcode",  
            };

            using (WinSCP.Session session = new WinSCP.Session())
            {
                // Connect
                session.Open(sessionOptions);
                var files = session.EnumerateRemoteFiles("/path/", "test*", WinSCP.EnumerationOptions.None);
                var directory = session.ListDirectory("/path/");

                foreach (var dir in files) {
                    Console.WriteLine(dir);
                }

                    // Upload files
                TransferOptions transferOptions = new TransferOptions();
                transferOptions.TransferMode = TransferMode.Binary;

                TransferOperationResult transferResult;
                transferResult =
                    session.GetFiles("/path/test*", @"D:\", false, transferOptions);

                // Throw on any error
                transferResult.Check();

                // Print results
                foreach (TransferEventArgs transfer in transferResult.Transfers)
                {
                    Console.WriteLine("Download of {0} succeeded", transfer.FileName);
                }
            }

        }
        catch (Exception e)
        {
            Console.Write

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM