Farhan Ahmed Saifi
Farhan Ahmed Saifi

Reputation: 93

Upload file to SFTP server using VB.NET

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.

Upvotes: 4

Views: 33081

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202672

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.

Upvotes: 5

Related Questions