List folders using FTP

To list all files and folders in a specific FTP folder just us ChangeFolder and GetList methods:

// C# version

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

    client.ChangeFolder("reports");

    List<FtpItem> items = client.GetList();

    foreach (FtpItem item in items)
    {
        Console.WriteLine("Name:        {0}", item.Name);
        Console.WriteLine("Size:        {0}", item.Size);
        Console.WriteLine("Modify date: {0}", item.ModifyDate);

        Console.WriteLine("Is folder:   {0}", item.IsFolder);
        Console.WriteLine("Is file:     {0}", item.IsFile);
        Console.WriteLine("Is symlink:  {0}", item.IsSymlink);

        Console.WriteLine();
    }

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

Using client As New Ftp()
	client.Connect("ftp.example.org")
	client.Login("user", "password")

	client.ChangeFolder("reports")

	Dim items As List(Of FtpItem) = client.GetList()

	For Each item As FtpItem In items
		Console.WriteLine("Name:        {0}", item.Name)
		Console.WriteLine("Size:        {0}", item.Size)
		Console.WriteLine("Modify date: {0}", item.ModifyDate)

		Console.WriteLine("Is folder:   {0}", item.IsFolder)
		Console.WriteLine("Is file:     {0}", item.IsFile)
		Console.WriteLine("Is symlink:  {0}", item.IsSymlink)

		Console.WriteLine()
	Next

	client.Close()
End Using

You can download Ftp.dll .NET FTP and FTPS library.

Get file or folder info using FTP

Server responses to FTP protocol’s LIST command are not well standardized. Ftp.dll FTP .NET component was programmed to handle almost any format of such response. This includes UNIX, Linux and Windows systems. Additionally if a server supports better standardized MLSD command, it is used instead of LIST.

To list all files and folders in current folder just use GetList method of Ftp class.
You’ll receive FtpItem collection you can easy iterate over. You can access each item’s information like: name, size, modification date by using FtpItem‘s properties.

// C# version

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

    List<FtpItem> items = client.GetList();

    foreach (FtpItem item in items)
    {
        Console.WriteLine("Name:        {0}", item.Name);
        Console.WriteLine("Size:        {0}", item.Size);
        Console.WriteLine("Modify date: {0}", item.ModifyDate);

        Console.WriteLine("Is folder:   {0}", item.IsFolder);
        Console.WriteLine("Is file:     {0}", item.IsFile);
        Console.WriteLine("Is symlink:  {0}", item.IsSymlink);

        Console.WriteLine();
    }

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

Using client As New Ftp()
	client.Connect("ftp.example.org")    ' or ConnectSSL for SSL
	client.Login("user", "password")

	Dim items As List(Of FtpItem) = client.GetList()

	For Each item As FtpItem In items
		Console.WriteLine("Name:        {0}", item.Name)
		Console.WriteLine("Size:        {0}", item.Size)
		Console.WriteLine("Modify date: {0}", item.ModifyDate)

		Console.WriteLine("Is folder:   {0}", item.IsFolder)
		Console.WriteLine("Is file:     {0}", item.IsFile)
		Console.WriteLine("Is symlink:  {0}", item.IsSymlink)

		Console.WriteLine()
	Next

	client.Close()
End Using

Getting basic file information using FTP

Follow code illustrates how to get basic information about specific file, such as size and last modification date, using FTP protocol:

// C# version

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

    long size = client.GetFileSize("reports/report_2010.txt");
    DateTime date = client.GetFileModificationTime("reports/report_2010.txt");

    Console.WriteLine("Size:        {0}", size);
    Console.WriteLine("Modify date: {0}", date);

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

Using client As New Ftp()
	client.Connect("ftp.example.org")
	client.Login("user", "password")

	Dim size As Long = client.GetFileSize("reports/report_2010.txt")
	Dim [date] As DateTime = client.GetFileModificationTime("reports/report_2010.txt")

	Console.WriteLine("Size:        {0}", size)
	Console.WriteLine("Modify date: {0}", [date])

	client.Close()
End Using

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

Rename file using FTP

To rename a file stored on the FTP server you just need use Rename method:

// C# version

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

    // You can rename files:
    client.Rename("reports/report.txt", "reports/report_2010.txt");

    // You can also rename folders using the same method:
    client.Rename("folder", "folder2");

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

Using client As New Ftp()
	client.Connect("ftp.example.org")
	client.Login("user", "password")

	' You can rename files:
	client.Rename("reports/report.txt", "reports/report_2010.txt")

	' You can also rename folders using the same method:
	client.Rename("folder", "folder2")

	client.Close()
End Using

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

Issue a custom command to FTP server

This article describes how to issue a custom command to FTP server using Ftp.dll – .NET FTP and FTPS library.

// C# version

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

    FtpResponse response = client.SendCommand("HELP");

    Console.WriteLine(response.Message);
    foreach (string line in response.Lines)
        Console.WriteLine(line);

    client.Close();
}

' VB.NET version

Using client As New Ftp()
	client.Connect("ftp.example.org")
	client.Login("user", "password")

	Dim response As FtpResponse = client.SendCommand("HELP")

	Console.WriteLine(response.Message)
	For Each line As String In response.Lines
		Console.WriteLine(line)
	Next

	client.Close()
End Using

The abowe code producess following output:


The following commands are recognized:
USER PASS QUIT CWD PWD PORT PASV TYPE
LIST REST CDUP RETR STOR SIZE DELE RMD
MKD RNFR RNTO ABOR SYST NOOP APPE NLST
MDTM XPWD XCUP XMKD XRMD NOP EPSV EPRT
AUTH ADAT PBSZ PROT FEAT MODE OPTS HELP
ALLO MLST MLSD SITE P@SW STRU CLNT MFMT
HASH

The other overload of SendCommand allows you to specify, if after negative response exception is thrown.

The next sample will not throw exception if the server does not recognize the command.
You can examine IsPositive property to check if the response was successful or not.

// C# version

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

    FtpResponse response = client.SendCommand("HELP", false);

    Console.WriteLine(response.IsPositive);
    Console.WriteLine(response.Message);
    foreach (string line in response.Lines)
        Console.WriteLine(line);


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

Using client As New Ftp()
	client.Connect("ftp.example.org")
	client.Login("user", "password")

	Dim response As FtpResponse = client.SendCommand("HELP", False)

	Console.WriteLine(response.IsPositive)
	Console.WriteLine(response.Message)
	For Each line As String In response.Lines
		Console.WriteLine(line)
	Next


	client.Close()
End Using

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

Use SSL with FTP (Explicit)

Explicit SSL uses the same port that regular FTP (21).

After regular connection, client explicitly asks the server to secure the connection. “AUTH TLS” command is used to do that.
As the SSL/TLS protocols self-negotiate their levels, there is no need to distinguish between SSL and TLS.

You should use AuthTLS method to enable TLS/SSL for both data channel and control channel:

// C# version

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

    client.AuthTLS();

    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.Connect("ftp.example.org")

    client.AuthTLS()

    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 21, you need to use overloaded version of Connect

// C# version

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

client.Connect("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.Connect("ftp.example.org");
    client.AuthTLS();
    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.Connect("ftp.example.org")
    client.AuthSSL()
    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

Here you can download Ftp.dll: .NET FTP/FTPS component.