c# - 在 C# 中使用 SSH.NET SFTP 下载目录

标签 c# .net sftp ssh.net

我正在使用 Renci.SSH 和 C# 从 Windows 计算机连接到我的 Unix 服务器。当目录内容只是文件时,我的代码按预期工作,但如果目录包含文件夹,我会得到这个

Renci.SshNet.Common.SshException: 'Failure'

这是我的代码,如何更新它以下载目录(如果存在)

private static void DownloadFile(string arc, string username, string password)
{
    string fullpath;
    string fp;
    var options = new ProgressBarOptions
    {
        ProgressCharacter = '.',
        ProgressBarOnBottom = true
    };

    using (var sftp = new SftpClient(Host, username, password))
    {
        sftp.Connect();
        fp = RemoteDir + "/" + arc;
        if (sftp.Exists(fp))     
            fullpath = fp;
        else
            fullpath = SecondaryRemoteDir + d + "/" + arc;

        if (sftp.Exists(fullpath))
        {
            var files = sftp.ListDirectory(fullpath);
            foreach (var file in files)
            {
                if (file.Name.ToLower().Substring(0, 1) != ".")
                {
                    Console.WriteLine("Downloading file from the server...");
                    Console.WriteLine();
                    using (var pbar = new ProgressBar(100, "Downloading " + file.Name + "....", options))
                    {
                        SftpFileAttributes att = sftp.GetAttributes(fullpath + "/" + file.Name);
                        var fileSize = att.Size;
                        var ms = new MemoryStream();
                        IAsyncResult asyncr = sftp.BeginDownloadFile(fullpath + "/" + file.Name, ms);
                        SftpDownloadAsyncResult sftpAsyncr = (SftpDownloadAsyncResult)asyncr;
                        int lastpct = 0;
                        while (!sftpAsyncr.IsCompleted)
                        {
                            int pct = (int)((long)sftpAsyncr.DownloadedBytes / fileSize) * 100;
                            if (pct > lastpct)
                                for (int i = 1; i < pct - lastpct; i++)
                                    pbar.Tick();
                        }
                        sftp.EndDownloadFile(asyncr);
                        Console.WriteLine("Writing File to disk...");
                        Console.WriteLine();
                        string localFilePath = "C:\" + file.Name;
                        var fs = new FileStream(localFilePath, FileMode.Create, FileAccess.Write);
                        ms.WriteTo(fs);
                        fs.Close();
                        ms.Close();
                    }
                }
            }
        }
        else
        {
            Console.WriteLine("The arc " + arc + " does not exist");
            Console.WriteLine();
            Console.WriteLine("Please press any key to close this window");
            Console.ReadKey();
        }
    }
}

最佳答案

BeginDownloadFile 下载一个文件。您不能使用它来下载文件夹。为此,您需要一一下载包含的文件。

为简单起见,以下示例使用同步下载(DownloadFile 而不是 BeginDownloadFile)。毕竟,无论如何,您都是在同步等待异步下载完成。要实现同步下载的进度条,请参阅 Displaying progress of file download in a ProgressBar with SSH.NET .

public static void DownloadDirectory(
    SftpClient sftpClient, string sourceRemotePath, string destLocalPath)
{
    Directory.CreateDirectory(destLocalPath);
    IEnumerable<SftpFile> files = sftpClient.ListDirectory(sourceRemotePath);
    foreach (SftpFile file in files)
    {
        if ((file.Name != ".") && (file.Name != ".."))
        {
            string sourceFilePath = sourceRemotePath + "/" + file.Name;
            string destFilePath = Path.Combine(destLocalPath, file.Name);
            if (file.IsDirectory)
            {
                DownloadDirectory(sftpClient, sourceFilePath, destFilePath);
            }
            else
            {
                using (Stream fileStream = File.Create(destFilePath))
                {
                    sftpClient.DownloadFile(sourceFilePath, fileStream);
                }
            }
        }
    }
}

关于c# - 在 C# 中使用 SSH.NET SFTP 下载目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52392766/

相关文章:

c# - 从 WPF 应用程序引用通用 Windows 库

C# 将多个参数传递给 BAT 文件

python - 在 ADLS 上自动激活 SFTP

java - 使用 Jsch 检查 SFTP 权限

python - 如何在python中使用opencv从sftp位置读取视频文件

c# - 如何在存储库中正确处理 Linq to SQL DataContext?

c# - 为所有泛型声明一次实现类型?

.net - 为什么 boolean 值比字符消耗更多的内存?

c# - 使用 C# 从 SQL 显示和保存 "time"数据类型?

c# - 将参数化的 TestFixture 移动到基类后,NUnit 测试没有结果。为什么?