I would like to download a file from SFTP using the SSH.NET library. However, I would like this file to be received in Byte array. Thus, this file must be stored in memory.
Here's how I do it
Sub Main()
Dim client As SftpClient = New SftpClient(hostname, username, password)
client.Connect()
Using b As System.IO.Stream = client.OpenRead("/www/Server.exe")
Dim data() As Byte = GetStreamAsByteArray(b)
End Using
End Sub
Public Shared Function GetStreamAsByteArray(ByVal stream As System.IO.Stream) As Byte()
Dim streamLength As Integer = Convert.ToInt32(stream.Length)
Dim fileData As Byte() = New Byte(streamLength) {}
' Read the file into a byte array
stream.Read(fileData, 0, streamLength)
stream.Flush()
stream.Close()
Return fileData
End Function
However this method does not work: indeed, by writing it on the disk to test it, it is corrupted.
Streamyou get fromOpenReadenough for most purposes?