Upload file to SFTP server using VB.NET

大憨熊 提交于 2021-02-07 08:00:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!