使用SSH.NET上传整个文件夹

mzsu5hc0  于 2023-05-30  发布在  .NET
关注(0)|答案(2)|浏览(245)

我在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));
        }
    }
}

这对于单个文件非常有效。现在我想上传整个文件夹/目录。
现在有没有人如何做到这一点?

nbysray5

nbysray51#

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

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 (var 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);
            }
        }
    }
}

如果你想要一个更简单的代码,你将不得不使用另一个库。例如,myWinSCP .NET assembly可以使用对Session.PutFilesToDirectory的单个调用来上传整个目录:

var results = session.PutFilesToDirectory(localPath, remotePath);
results.Check();
jyztefdp

jyztefdp2#

如果有帮助,我已经将代码翻译成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

相关问题