问题
I need to upload a file to SFTP server. I am using VB.NET 2008.
How can I upload a simple .csv
file from my local computer to SFTP server using port number, user name and password, etc? Thanks in advance.
回答1:
A commonly used open source SFTP library for .NET is SSH.NET.
With it, you can use a code like this:
Dim client As SftpClient = New SftpClient("example.com", "username", "password")
client.Connect()
Using stream As Stream = File.OpenRead("C:\local\path\some.csv")
client.UploadFile(stream, "/remote/path/some.csv")
End Using
There are other libraries too. If you need more high-level functions, like uploading all files in a directory or even complete directory structures, you may find my WinSCP .NET assembly useful.
With WinSCP, you can use a code like this to upload all .csv files:
Dim sessionOptions As New SessionOptions
With sessionOptions
.Protocol = Protocol.Sftp
.HostName = "example.com"
.UserName = "username"
.UserName = "password"
.SshHostKeyFingerprint = "ssh-rsa 2048 ..."
End With
Using session As New Session
session.Open(sessionOptions)
session.PutFiles("C:\local\path\*.csv", "/remote/path/*").Check()
End Using
WinSCP GUI can generate an upload code template, like the one above, for you.
Though, WinSCP .NET assembly is not a native .NET library, it's just a .NET wrapper around a console application. So it has its own limitations.
来源:https://stackoverflow.com/questions/48556236/upload-file-to-sftp-server-using-vb-net