我想使用 SSH.NET 库从 SFTP 下载文件。但是,我希望这个文件以Byte数组的形式接收。因此,该文件必须存储在内存中。
这是我的做法
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
但是这种方法不起作用:实际上,通过将其写入磁盘进行测试,它已损坏。