This project is provided as an example of how to send files securely to an SFTP server using VB.NET and WinSCP.
This project is comprised of 3 main components that would need to replicated in a production software environment to work:
- SFTP Binary Library (WinSCP)
- SftpService.vb (SFTP Service/Wrapper Code)
- Application logic (sample ConsoleApp.vb in project)
WinSCP .NET assembly / COM library offers the library to be used to facilitate connection over SFTP. It is leveraged by the wrapper service for ease of use. These files must be present in the project to work:
- WinSCP.exe
- WinSCPnet.dll
Installation instructions:
- Copy both files into project, make sure they are included in project
- Right click on References in Solutions Explorer -> click "Add Reference"
- Click on "Browse", locate .dll and add to project
- Right click on each file in solution explorer -> click "Properties"
- Edit "Copy to Output Directory" and set to "Copy if newer"
These files must be in the final output in the same directory, you need to right click -> Properties, then set "Copy to Output Directory" to one of the "Copy ..." options.
This is the service class to copy and use as-is to provide the SFTP functionality with WinSCP
The included example key has this public thumbprint to set on your server for testing: ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAQEAniLOkUwxAVBayxWbODbPrTZ7pU/dkIQDj6VRZeAbUT+vChbZs3tuDO7oMTKl35ALWIim8/uHU2T62+tyNxZo7thuAYg4VfX9EBgOb7G8/HZQ6yamiX/xADP4y4Tci9I9DoLqf62XRu8dq72GXDzZUG668053K7iJRvMbTwnE6Hx1iIhoK44whHJYvA2ub8SAzJ2gJbU3xY/OCN8HFJL2KnjBVXo4pu0O3Tnbi7vmkqyod9Y01oXHztfnbm/j1kcDEUkqVAY1rfzQ0xGLo9SnFPcB2kiX/xHOM+4INUP6qrjAMhOVziSQRM1nBrsTZhtF82M122vBShonTcMObm+kpQ== Example Key
Path to certificate must be complete and app must have access to path/file
Dim keyFileLocation = Directory.GetCurrentDirectory() + "\" + "key.ppk"
Certificate that requires passphrase:
Dim sftpService = New SftpService("sftp.myserver.com", "username", keyFileLocation, "ThisIsMyP@ssword")
Path to certificate must be complete and app must have access to path/file
Dim keyFileLocation = Directory.GetCurrentDirectory() + "\" + "key-nopass.ppk"
Certificate that does not have a passphrase:
Dim sftpService = New SftpService("sftp.myserver.com", "username", keyFileLocation, Nothing)
Dim sftpService = New SftpService("sftp.myserver.com", "username", "ThisIsMyP@ssword")
Uploading file to server:
Dim result = sftpService.UploadFile("..\..\README.md")
Listing files in current directory:
Dim files As RemoteFileInfoCollection = sftpService.ListFiles()
For Each fileInfo In files
Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}", _
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime)
Next
Listing files in specific directory:
Dim files As RemoteFileInfoCollection = sftpService.ListFiles("/home/user")
For Each fileInfo In files
Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}", _
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime)
Next
Deleting a file from the server:
Dim result = sftpService.DeleteFile("README.md")
This shows a sample of how to interact with the SftpService class, it represents sample control code you would instead have in a production application