gpt4 book ai didi

c# - 使用 SSH.NET 从 SFTP 服务器下载一个特定文件

转载 作者:行者123 更新时间:2023-12-02 14:12:13 37 4
gpt4 key购买 nike

我正在尝试使用 SSH.NET 从服务器仅下载一个文件。

到目前为止我有这个:

using Renci.SshNet;
using Renci.SshNet.Common;
...
public void DownloadFile(string str_target_dir)
{
client.Connect();
if (client.IsConnected)
{
var files = client.ListDirectory(@"/home/xymon/data/hist");
foreach (SftpFile file in files)
{
if (file.FullName== @"/home/xymon/data/hist/allevents")
{
using (Stream fileStream = File.OpenWrite(Path.Combine(str_target_dir, file.Name)))
{
client.DownloadFile(file.FullName, fileStream);
}
}
}
}
else
{
throw new SshConnectionException(String.Format("Can not connect to {0}@{1}",username,host));
}
}

我的问题是我不知道如何构建 SftpFile使用字符串 @"/home/xymon/data/hist/allevents" .

这就是我使用 foreach 的原因循环条件。

感谢您的帮助。

最佳答案

您不需要 SftpFile 来调用 SftpClient.DownloadFile。该方法仅采用普通路径:

/// <summary>
/// Downloads remote file specified by the path into the stream.
/// </summary>
public void DownloadFile(string path, Stream output, Action<ulong> downloadCallback = null)

像这样使用它:

using (Stream fileStream = File.OpenWrite(Path.Combine(str_target_dir, "allevents")))
{
client.DownloadFile("/home/xymon/data/hist/allevents", fileStream);
}

如果你真的需要SftpFile,你可以使用SftpClient.Get方法:

/// <summary>
/// Gets reference to remote file or directory.
/// </summary>
public SftpFile Get(string path)

但你没有。

关于c# - 使用 SSH.NET 从 SFTP 服务器下载一个特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40612788/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com