Default ports for FTP

By default FTP uses port 21 for control channel:

// C# version

using (Ftp client = new Ftp())
{
    client.Connect("ftp.example.org");

    //...

    client.Close();
}
' VB.NET version

Using client As New Ftp()
	client.Connect("ftp.example.org")

	'...

	client.Close()
End Using

By default FTPS uses port 990 for control channel:

// C# version

using (Ftp client = new Ftp())
{
    client.ConnectSSL("ftp.example.org");

    //...

    client.Close();
}
' VB.NET version

Using client As New Ftp()
	client.ConnectSSL("ftp.example.org")

	'...

	client.Close()
End Using

If your FTP or FTPS server is running on different port just use overladed version of Connect and ConnectSSL:

// C# version

client.Connect("ftp.example.org", 999);
client.ConnectSSL("ftp.example.org", 999);
' VB.NET version

client.Connect("ftp.example.org", 999)
client.ConnectSSL("ftp.example.org", 999)

You can download Ftp.dll FTP/FTPS library for .NET here.

Use SSL with FTP (FTPS)

Using FTPS (FTP protocol over secure SSL channel is easy with Ftp.dll .NET FTPS component. The only difference compared to the FTP protocol is that you need to use ConnectSSL method instead of regular Connect:

// C# version

using (Ftp client = new Ftp())
{
    client.ConnectSSL("ftp.example.org");
    client.Login("username", "password");

    foreach (FtpItem item in client.GetList())
    {
        if (item.IsFolder == true)
            Console.WriteLine("[{0}]", item.Name);
        else
            Console.WriteLine("{0}", item.Name);
    }
    client.Close();
}
' VB.NET version

Using client As New Ftp()

    client.ConnectSSL("ftp.example.org")
    client.Login("username", "password")

    For Each item As FtpItem In client.GetList()
        If item.IsFolder = True Then
            Console.WriteLine("[{0}]", item.Name)
        Else
            Console.WriteLine("{0}", item.Name)
        End If
    Next
    client.Close()
End Using

If your FTP server is using other port than standard 990, you need to use overloaded version of ConnectSSL

// C# version

client.ConnectSSL("ftp.example.org", 999);
' VB.NET version

client.ConnectSSL("ftp.example.org", 999)

The last sample shows how to deal with self-signed certificates:

// C# version

using (Ftp client = new Ftp())
{
    // Use this line to validate self-signed certificates:
    client.ServerCertificateValidate += ValidateCertificate;

    client.ConnectSSL("ftp.example.org");
    client.Login("username", "password");

    foreach (FtpItem item in client.GetList())
    {
        if (item.IsFolder == true)
            Console.WriteLine("[{0}]", item.Name);
        else
            Console.WriteLine("{0}", item.Name);
    }
    client.Close();
}

private static void ValidateCertificate(
    object sender,
    ServerCertificateValidateEventArgs e)
{
    const SslPolicyErrors ignoredErrors =
        SslPolicyErrors.RemoteCertificateChainErrors |
        SslPolicyErrors.RemoteCertificateNameMismatch;

    if ((e.SslPolicyErrors & ~ignoredErrors) == SslPolicyErrors.None)
    {
        e.IsValid = true;
        return;
    }
    e.IsValid = false;
}
' VB.NET version

Using client As New Ftp()
    ' Use this line to validate self-signed certificates:
    AddHandler client.ServerCertificateValidate, AddressOf ValidateCerificate

    client.ConnectSSL("ftp.example.org")
    client.Login("username", "password")

    For Each item As FtpItem In client.GetList()
        If item.IsFolder = True Then
            Console.WriteLine("[{0}]", item.Name)
        Else
            Console.WriteLine("{0}", item.Name)
        End If
    Next
    client.Close()
End Using

Private Sub ValidateCerificate( _
    ByVal sender As Object, _
    ByVal e As ServerCertificateValidateEventArgs)

    Const ignoredErrors As SslPolicyErrors = _
        SslPolicyErrors.RemoteCertificateChainErrors Or _
        SslPolicyErrors.RemoteCertificateNameMismatch

    If (e.SslPolicyErrors And Not ignoredErrors) = SslPolicyErrors.None Then
        e.IsValid = True
        Return
    End If
    e.IsValid = False
End Sub

You can download Ftp.dll FTP/FTPS client for .NET here.

Upload 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 uploads the file from disk to reports folder on the server, using FTP protocol:

// C# version

using (Ftp client = new Ftp())
{
    client.Connect("ftp.example.com");    // or ConnectSSL for SSL
    client.Login("user", "password");

    client.Upload(@"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.Upload("reports\report.txt", "c:\report.txt")

	client.Close()
End Using

You can also upload the file from memory:

// C# version

using (Ftp client = new Ftp())
{
    client.Connect("ftp.example.com");    // or ConnectSSL for SSL
    client.Login("user", "password");

    byte[] bytes = Encoding.Default.GetBytes("Hello from Ftp.dll");
    client.Upload(@"reports\report.txt", bytes);

    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() = Encoding.[Default].GetBytes("Hello from Ftp.dll")
	client.Upload("reportsreport.txt", bytes)

	client.Close()
End Using

Finally you can also use overload that uses any Stream as the source.

You can download Ftp.dll FTP/FTPS client for .NET here.

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.

You can download Ftp.dll FTP/FTPS client for .NET here.

Logging in Ftp.dll

To enable logging for Ftp.dll .NET FTP and FTPS component you only need to add the following line before you connect:

// C# version:

Limilabs.FTP.Log.Enabled = true;

' VB.NET version:

Limilabs.FTP.Log.Enabled = True

You can observe the log output by:

  • looking at the Visual Studio’s output window (View/Output/’Show output from’: Debug)
  • subscribing to Log.WriteLine event
  • defining custom listeners using your application’s config file (App.config or Web.config)
  • using log4net

This is how the log looks like in the Visual Studio’s output window:

You can also enable logging using your application’s config file (App.config, Web.config):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>

      <switches>
        <add name="Ftp.dll" value="Verbose" />
      </switches>

    </system.diagnostics>
</configuration>

log4net

If you are using the latest version of log4net.dll, Ftp.dll is going to use log4net instead of standard .NET System.Net.TraceSource class. Please refer to log4net manual on how to capture log entries.

Ftp.dll uses logger called “Ftp.dll” (_logger = LogManager.GetLogger(“Ftp.dll”)) and level Info (_logger.Info(message)) to log information.

Please remember that even when using log4net, you need to enable logging by setting “Limilabs.FTP.Log.Enabled = True” or by setting Ftp.dll trace switch in the config file (App.config, Web.config) to Verbose as shown above.

Log to file

You’ll need to define a TextWriterTraceListener that writes to a file and connect it with Ftp.dll trace source. The easiest solution is to modify your application’s config file (App.config, Web.config) accordingly:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.diagnostics>

        <trace autoflush="true"/>
   
        <switches>
            <add name="Ftp.dll" value="Verbose"/>
        </switches>
   
        <sources>
            <source name="Ftp.dll">
                <listeners>
                    <add name="FtpLogFile"/>
                </listeners>
            </source>
        </sources>
   
        <sharedListeners>
            <add 
                name="FtpLogFile" 
                type="System.Diagnostics.TextWriterTraceListener" 
                initializeData="c:\folder-with-write-access\ftp.log"/>
        </sharedListeners>

    </system.diagnostics>
</configuration>

Log.WriteLine

Log class exposes WriteLine event. You can use that event to subscribe your own logging library.

// C#

Limilabs.FTP.Log.WriteLine += Console.WriteLine;
' VB.NET

Limilabs.FTP.Log.WriteLine += Console.WriteLine

You can download Ftp.dll FTP/FTPS client for .NET here.