SSH.NET上传整个文件夹

时间:2016-09-08 18:21:03

标签: c# ssh sftp ssh.net

我在C#2015中使用SSH.NET。

使用此方法,我可以将文件上传到我的SFTP服务器。

public void upload()
{
    const int port = 22;
    const string host = "*****";
    const string username = "*****";
    const string password = "*****";
    const string workingdirectory = "*****";
    string uploadfolder = @"C:\test\file.txt";

    Console.WriteLine("Creating client and connecting");
    using (var client = new SftpClient(host, port, username, password))
    {
        client.Connect();
        Console.WriteLine("Connected to {0}", host);

        client.ChangeDirectory(workingdirectory);
        Console.WriteLine("Changed directory to {0}", workingdirectory);

        using (var fileStream = new FileStream(uploadfolder, FileMode.Open))
        {
            Console.WriteLine("Uploading {0} ({1:N0} bytes)",
                                uploadfolder, fileStream.Length);
            client.BufferSize = 4 * 1024; // bypass Payload error large files
            client.UploadFile(fileStream, Path.GetFileName(uploadfolder));
        }
    }
}

适用于单个文件。现在我想上传整个文件夹/目录。

现在有人如何实现这个目标?

2 个答案:

答案 0 :(得分:7)

没有神奇的方式。您必须枚举文件并逐个上传:

void UploadDirectory(SftpClient client, string localPath, string remotePath)
{
    Console.WriteLine("Uploading directory {0} to {1}", localPath, remotePath);

    IEnumerable<FileSystemInfo> infos =
        new DirectoryInfo(localPath).EnumerateFileSystemInfos();
    foreach (FileSystemInfo info in infos)
    {
        if (info.Attributes.HasFlag(FileAttributes.Directory))
        {
            string subPath = remotePath + "/" + info.Name;
            if (!client.Exists(subPath))
            {
                client.CreateDirectory(subPath);
            }
            UploadDirectory(client, info.FullName, remotePath + "/" + info.Name);
        }
        else
        {
            using (Stream fileStream = new FileStream(info.FullName, FileMode.Open))
            {
                Console.WriteLine(
                    "Uploading {0} ({1:N0} bytes)",
                    info.FullName, ((FileInfo)info).Length);

                client.UploadFile(fileStream, remotePath + "/" + info.Name);
            }
        }
    }
}

答案 1 :(得分:-3)

如果有帮助,我已经将代码翻译成VB.NET:

Sub UploadDirectorySftp(Client As SftpClient, LocalPath As String, RemotePath As String)

    Dim subPath As String
    Dim Infos As IEnumerable(Of FileSystemInfo) =
        New DirectoryInfo(LocalPath).EnumerateFileSystemInfos()
    For Each info In Infos
        If info.Attributes.HasFlag(FileAttributes.Directory) Then
            subPath = RemotePath + "/" + info.Name
            If Not Client.Exists(subPath) Then
                Client.CreateDirectory(subPath)
            End If
            UploadDirectorySftp(Client, info.FullName, RemotePath + "/" + info.Name)
        Else
            Using filestream = New FileStream(info.FullName, FileMode.Open)
                Client.UploadFile(filestream, RemotePath + "/" + info.Name)
            End Using
        End If
    Next

End Sub

使用示例:

Using ServerClient = New Renci.SshNet.SftpClient("host", porta, "user", "pswd")
    ServerClient.Connect()
    UploadDirectorySftp(ServerClient, "V:\aaa\", "Immagini")
    ServerClient.Disconnect()
End Using