bircastri
bircastri

Reputation: 2169

How to download file from SFTP in vb.net

I am trying to use the classes in Renci.SshNet.Sftp to download a file from an SFTP server with VB.NET. Here is my code:

Using client As New SftpClient("server", "test", "test")
    client.Connect()
    Dim list As List(Of SftpFile) = CType(client.ListDirectory(""), List(Of SftpFile))
    '------------------------
    For Each sFile As SftpFile In list
        Console.WriteLine(sFile.Name)
        client.DownloadFile("path", ????)
    Next
    client.Disconnect()
End Using

With this code I can connect to the server and see the file, but I can't download it. I don't know how to call the DownloadFile method.

Upvotes: 2

Views: 8005

Answers (1)

Steven Doggart
Steven Doggart

Reputation: 43743

The second parameter of the DownloadFile method takes a stream. So, you just need to create a new FileStream to write the downloaded data to a new file, like this:

Using fs As New FileStream(localFilePath, FileMode.CreateNew, FileAccess.Write)
    client.DownloadFile(serverFilePath, fs)
End Using

Upvotes: 3

Related Questions