使用SftpClient时出现Renci.SshNet.Common.SshOperationTimeoutException,目前我只能使用SshClient,有没有办法使用SshClient下载和上传文件?有人能对此有所启发吗?
我在窗口命令提示符下尝试了ftp 10.101.10.10,它可以正常工作。文件只有1KB。
//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();
}
答案 0 :(得分:2)
不知道为什么,但是我确实尝试过WinSCP,并且可以正常工作。
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