Download file using FTP
First you need to add reference to Ftp.dll .NET FTP library, and import appropriate namespaces:
// C# version using Limilabs.FTP.Client;
' VB.NET version Imports Limilabs.FTP.Client
The following code downloads the file from reports folder on the server, using FTP protocol, and saves it to disk:
// C# version using (Ftp client = new Ftp()) { client.Connect("ftp.example.com"); // or ConnectSSL for SSL client.Login("user", "password"); client.Download(@"reports\report.txt", @"c:\report.txt"); client.Close(); }
' VB.NET version Using client As New Ftp() client.Connect("ftp.example.com") ' or ConnectSSL for SSL client.Login("user", "password") client.Download("reports\report.txt", "c:\report.txt") client.Close() End Using
You can also download the file to memory and process it immediately:
// C# version using (Ftp client = new Ftp()) { client.Connect("ftp.example.com"); // or ConnectSSL for SSL client.Login("user", "password"); byte[] bytes = client.Download(@"reports/report.txt"); string report = Encoding.Default.GetString(bytes); Console.Write(report); client.Close(); }
' VB.NET version Using client As New Ftp() client.Connect("ftp.example.com") ' or ConnectSSL for SSL client.Login("user", "password") Dim bytes As Byte() = client.Download("reports/report.txt") Dim report As String = Encoding.[Default].GetString(bytes) Console.Write(report) client.Close() End Using
Finally you can also use the overload that uses any Stream as the destination.