Search code examples
vb.netsslftpftps

VB.NET 2010 - FTPs TLS Explicit Mode


Context: I am trying to make an application that will store (when desired) the application configuration file for the user on an FTPs server in TLS Explicit mode.

Server: FileZilla Server for Windows

I have not been able to figure out the Cryptography and Certification handling in VB yet. Been working on it for a week now and scrapped everything to start fresh.

To start from the beginning, here is what I've got. Any help would be greatly appreciated!

Thank you!

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim UPReq As System.Net.FtpWebRequest = DirectCast(System.Net.WebRequest.Create("ftp://127.0.0.1/app/Test_File.txt"), System.Net.FtpWebRequest)
    UPReq.Credentials = New System.Net.NetworkCredential("FTPusername", "FTPpassword")
    'UPReq.ClientCertificates
    UPReq.EnableSsl = True
    UPReq.Method = System.Net.WebRequestMethods.Ftp.UploadFile
    Dim file() As Byte = System.IO.File.ReadAllBytes("C:\Users\" + Environment.UserName + "\Desktop\Test_File.txt")
    Dim Upload As System.IO.Stream = UPReq.GetRequestStream()
    Upload.Write(file, 0, file.Length)
    Upload.Close()
    Upload.Dispose()
End Sub

Solution

  • For anyone reading this in the future, USE WinSCP!

    Their guides are amazing!

    This is for all the current/future programmers! In Visual Studio, go to Tools > Extension Manager... then install NuGet Package Manager, Restart Visual Studio.

    Download and install WinSCP, then download the WinSCP NuGet package from HERE. In Visual Studio right click on your project in the Solution Explorer then click on Manage NuGet Packages... then click on Settings in the bottom left. Add the source (folder where you downloaded the WinSCP NuGet Package) then click OK.

    Next, you will see WinSCP .NET assembly show up, click on it and hit Install. Then make sure to add Imports WinSCP to the top of your project code.

    After that you can code the functions for Connecting, Uploading, and Downloading. Use their examples provided HERE

    MASSIVE TIP: Use WinSCP to connect to your FTPS server, then click on the Session tab and click on Generate session URL/code. Inside the new window that appears, select the .NET assembly code tab. Then make sure your programming language is selected in the Language drop down. There it is! It will show you the session connection code for your FTPS server, just copy and paste that into Visual Studio.

    From there, you will have to do the rest to modify their example code, and combine it with the session connection code that WinSCP gave you.

    Happy Coding!