尝试使用 SftpClient 下载具有 Renci.SshNet 的文件时出现 'No such file' 错误
Getting 'No such file' error when trying to download a file with Renci.SshNet using SftpClient
当我从另一台服务器下载文件时它起作用了。我可以删除文件并获取目录列表,但我无法下载文件。我得到的唯一例外是 'No such file',即使我使用从 ListDirectory 调用返回的文件也是如此。工作的服务器安装了 SSH 密钥,而没有安装 SSH 密钥的服务器。 Renci.SshNet 只有 ftp 来自安装了 SSH 的服务器吗?
private void button7_Click(object sender, EventArgs e)
{
string host = "ether4";
string pathLocalFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "factor_out.txt");
using (SftpClient sftp = new SftpClient(host, _username, _password))
{
try
{
sftp.Connect();
var files = sftp.ListDirectory("/factor/dktest/pdi");
sftp.ChangeDirectory("/factor/dktest/pdi");
Debug.Print(sftp.WorkingDirectory); // <-- /factor/dktest/pdi
foreach (var file in files)
{
using (Stream fileStream = File.OpenWrite(pathLocalFile))
{
//if (!file.Name.StartsWith("."))
// sftp.DownloadFile(file.Name, fileStream); // <-- Exception thrown here
if (!file.FullName.EndsWith("."))
{
sftp.DownloadFile(file.FullName, fileStream); // <-- Exception thrown here
break;
}
}
}
sftp.Disconnect();
}
catch (Exception er)
{
Console.WriteLine("An exception has been caught " + er.ToString()); // No Such File
}
}
}
完整的错误信息是"No such file"。
没有 InnerException。 HResult = -2146233088.
file.FullName = "/factor/dktest/pdi/gl_accounts.txt"
StackTrace
at Renci.SshNet.Common.AsyncResult.EndInvoke()
at Renci.SshNet.Sftp.SftpSession.EndOpen(SftpOpenAsyncResult asyncResult)
at Renci.SshNet.ServiceFactory.CreateSftpFileReader(String fileName, ISftpSession sftpSession, UInt32 bufferSize)
at Renci.SshNet.SftpClient.InternalDownloadFile(String path, Stream output, SftpDownloadAsyncResult asyncResult, Action`1 downloadCallback)
at Renci.SshNet.SftpClient.DownloadFile(String path, Stream output, Action`1 downloadCallback)
at FtpTesting.frmSshNet.button7_Click(Object sender, EventArgs e) in C:\Development\FtpTesting\FtpTesting\frmSshNet.cs:line 98"
您标记了您的问题 ftp,但您使用的是 SFTP 代码。
虽然它不能真正解决您眼前的问题,但您似乎确实需要使用 FTP。您的 SFTP 服务器可能因某种原因损坏了。
SSH.NET不支持FTP。您可以使用 .NET FtpWebRequest
或某些第 3 方 FTP 客户端库。
当我从另一台服务器下载文件时它起作用了。我可以删除文件并获取目录列表,但我无法下载文件。我得到的唯一例外是 'No such file',即使我使用从 ListDirectory 调用返回的文件也是如此。工作的服务器安装了 SSH 密钥,而没有安装 SSH 密钥的服务器。 Renci.SshNet 只有 ftp 来自安装了 SSH 的服务器吗?
private void button7_Click(object sender, EventArgs e)
{
string host = "ether4";
string pathLocalFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "factor_out.txt");
using (SftpClient sftp = new SftpClient(host, _username, _password))
{
try
{
sftp.Connect();
var files = sftp.ListDirectory("/factor/dktest/pdi");
sftp.ChangeDirectory("/factor/dktest/pdi");
Debug.Print(sftp.WorkingDirectory); // <-- /factor/dktest/pdi
foreach (var file in files)
{
using (Stream fileStream = File.OpenWrite(pathLocalFile))
{
//if (!file.Name.StartsWith("."))
// sftp.DownloadFile(file.Name, fileStream); // <-- Exception thrown here
if (!file.FullName.EndsWith("."))
{
sftp.DownloadFile(file.FullName, fileStream); // <-- Exception thrown here
break;
}
}
}
sftp.Disconnect();
}
catch (Exception er)
{
Console.WriteLine("An exception has been caught " + er.ToString()); // No Such File
}
}
}
完整的错误信息是"No such file"。 没有 InnerException。 HResult = -2146233088.
file.FullName = "/factor/dktest/pdi/gl_accounts.txt"
StackTrace
at Renci.SshNet.Common.AsyncResult.EndInvoke()
at Renci.SshNet.Sftp.SftpSession.EndOpen(SftpOpenAsyncResult asyncResult)
at Renci.SshNet.ServiceFactory.CreateSftpFileReader(String fileName, ISftpSession sftpSession, UInt32 bufferSize)
at Renci.SshNet.SftpClient.InternalDownloadFile(String path, Stream output, SftpDownloadAsyncResult asyncResult, Action`1 downloadCallback)
at Renci.SshNet.SftpClient.DownloadFile(String path, Stream output, Action`1 downloadCallback)
at FtpTesting.frmSshNet.button7_Click(Object sender, EventArgs e) in C:\Development\FtpTesting\FtpTesting\frmSshNet.cs:line 98"
您标记了您的问题 ftp,但您使用的是 SFTP 代码。
虽然它不能真正解决您眼前的问题,但您似乎确实需要使用 FTP。您的 SFTP 服务器可能因某种原因损坏了。
SSH.NET不支持FTP。您可以使用 .NET FtpWebRequest
或某些第 3 方 FTP 客户端库。