Examples

This page contains a list of examples that demonstrate how to use FTPClient.NET. All examples are shown both in C# and VB.NET

Methods for Files Methods for Folders

Upload File

This example demonstrates how to upload a local file to the current remote folder.

C#

using System;
using KetLabs.FTP;
 
class UploadFile
{
 public static void Demo()
 {
   // connect
   FtpClient ftp = new FtpClient();
   ftp.Username = "USERNAME";
   ftp.Password = "PASSWORD";
   ftp.Connect("HOST");
 
   // upload
   ftp.Upload(@"c:\test.txt", "test.txt");
 
   ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class UploadFile
  Public Shared Sub Demo()
        ' connect
	Dim ftp As New FtpClient()
	ftp.Username = "USERNAME"
	ftp.Password = "PASSWORD"
	ftp.Connect("HOST")
 
        ' upload
	ftp.Upload("c:\test.txt", "test.txt")
 
	ftp.Disconnect()
  End Sub
End Class

Back to Top

Download file

The example below demonstrates how to download a remote file from the current remote folder to a local destination. If you wish to specify an absolute remote path prefix the remote folder location with /
C#

using System;
using KetLabs.FTP;
 
class DownloadFile
{
 public static void Demo()
 {
        // connect
	FtpClient ftp = new FtpClient();
	ftp.Username = "USERNAME";
	ftp.Password = "PASSWORD";
	ftp.Connect("HOST");
 
        // download
	ftp.Download("test.txt", @"LocalFiles\test1.txt");
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class DownloadFile
 Public Shared Sub Demo()
        ' connect
	Dim ftp As New FtpClient()
	ftp.Username = "USERNAME"
	ftp.Password = "PASSWORD"
	ftp.Connect("HOST")
 
        ' download
	ftp.Download("test.txt", "LocalFiles\test1.txt")
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Rename remote file

This example shows how to rename a remote file.

C#

using System;
using KetLabs.FTP;
 
class RenameFile
{
 public static void Demo()
 {
        // connect
	FtpClient ftp = new FtpClient();
	ftp.Username = "USERNAME";
	ftp.Password = "PASSWORD";
	ftp.Connect("HOST");
 
        // rename
	ftp.Rename("test.txt", "test1.txt");
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class RenameFile
 Public Shared Sub Demo()
        ' connect
	Dim ftp As New FtpClient()
	ftp.Username = "USERNAME"
	ftp.Password = "PASSWORD"
	ftp.Connect("HOST")
 
        ' rename
	ftp.Rename("test.txt", "test1.txt")
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Delete file

This example demonstrates how to delete a remote file. The example assumes that the specified remote file is located in the current remote folder. To delete a file that is located in a different folder from the current, use an absolute path starting with / (for example: /public_html/myfolder/file_to_be_deleted.html)

C#

using System;
using KetLabs.FTP;
 
class DeleteFile
{
 public static void Demo()
 {
        // connect
	FtpClient ftp = new FtpClient();
	ftp.Username = "USERNAME";
	ftp.Password = "PASSWORD";
	ftp.Connect("HOST");
 
        // delete file
	ftp.Delete("test1.txt");
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class DeleteFile
 Public Shared Sub Demo()
        ' connect
	Dim ftp As New FtpClient()
	ftp.Username = "USERNAME"
	ftp.Password = "PASSWORD"
	ftp.Connect("HOST")
 
       ' delete file
	ftp.Delete("test1.txt")
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Check that a remote file exists

The example below demonstrates how to check that a remote file exists.

C#

using System;
using KetLabs.FTP;
 
class FileExists
{
 public static void Demo()
 {
    // connect
    FtpClient ftp = new FtpClient();
    ftp.Username = "USERNAME";
    ftp.Password = "PASSWORD";
    ftp.Connect("HOST");
 
    // check existence
    bool exists = ftp.FileExists("test.txt");
    Console.WriteLine(exists);
 
    ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class FileExists
 Public Shared Sub Demo()
    ' connect
    Dim ftp As New FtpClient()
    ftp.Username = "USERNAME"
    ftp.Password = "PASSWORD"
    ftp.Connect("HOST")
 
    ' check existence
    Dim exists As Boolean = ftp.FileExists("test.txt")
    Console.WriteLine(exists)
 
    ftp.Disconnect()
 End Sub
End Class

Back to Top


Check file size

This example shows how to get the size in bytes of a remote file.

C#

using System;
using KetLabs.FTP;
 
class FileSize
{
 public static void Demo()
 {
    // connect
    FtpClient ftp = new FtpClient();
    ftp.Username = "USERNAME";
    ftp.Password = "PASSWORD";
    ftp.Connect("HOST");
 
    // get file size
    long sizeInBytes = ftp.FileSize("test.txt");
    if (sizeInBytes > -1)
    {
	Console.WriteLine("File size in bytes: " + sizeInBytes);
    } else {
 	Console.WriteLine("The specified file does not exist.");
    }
 
    ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class FileSize
 Public Shared Sub Demo()
    ' connect
    Dim ftp As New FtpClient()
    ftp.Username = "USERNAME"
    ftp.Password = "PASSWORD"
    ftp.Connect("HOST")
 
    ' get file size
    Dim sizeInBytes As Long = ftp.FileSize("test.txt")
    If sizeInBytes > -1 Then
	Console.WriteLine("File size in bytes: " + sizeInBytes)
    Else
	Console.WriteLine("The specified file does not exist.")
    End If
 
    ftp.Disconnect()
 End Sub
End Class

Back to Top


List files in a remote folder

This example demonstrates how to list the contents of a sub folder of the current remote folder. To list the current remote folder invoke the method ListCurrentDirectory. You can also specify an absolute remote folder path by using a / prefix in front of the folder path string.

C#

using System;
using KetLabs.FTP;
 
class ListDirectory
{
 public static void Demo()
 {
    // connect
    FtpClient ftp = new FtpClient();
    ftp.Username = Helper.USERNAME;
    ftp.Password = Helper.PASSWORD;
    ftp.Connect(Helper.HOST);
 
    // list directory
    FtpFileInfo[] files = ftp.ListDirectory("remote_dir");
    Helper.Print(files);
 
    ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class ListDirectory
 Public Shared Sub Demo()
	' connect
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	' list directory
	Dim files As FtpFileInfo() = ftp.ListDirectory("remote_dir")
	Helper.Print(files)
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Upload folder

The example below shows how to upload a whole local folder. When we upload a folder there is an additional parameter that specifies should sub folders be uploaded too. We can also invoke the plain Upload method, which will be equivalent to a recursive folder upload.

C#

using System;
using KetLabs.FTP;
 
class UploadFolder
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	bool recursive = true;
	ftp.UploadDirectory("LocalFiles", "RemoteFiles", recursive);
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class UploadFolder
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	Dim recursive As Boolean = True
	ftp.UploadDirectory("LocalFiles", "RemoteFiles", recursive)
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Download remote folder

This example demonstrates how to download a whole remote folder. Like the upload folder example above there is a third parameter that specifies should remote sub folders be downloaded too. Again using the plain Download method is equivalent to DownloadDirectory with the recursive parameter set to true.

C#

using System;
using KetLabs.FTP;
 
class DownloadDirectory
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	bool recursive = true;
	ftp.DownloadDirectory("RemoteFiles", "LocalFiles", recurse);
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class DownloadDirectory
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	Dim recursive As Boolean = True
	ftp.DownloadDirectory("RemoteFiles", "LocalFiles", recursive)
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Rename directory

Renaming a remote folder is equivalent to renaming a remote file. See this example for details:

C#

using System;
using KetLabs.FTP;
 
class RenameDirectory
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	ftp.Rename("RemoteFiles", "RemoteFilesNew");
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class RenameDirectory
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	ftp.Rename("RemoteFiles", "RemoteFilesNew")
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Delete remote folder

We can delete a remote folder with the Delete method used for files, which is equivalent to DeleteDirectory with the recursive parameter set to true.

C#

using System;
using KetLabs.FTP;
 
class DeleteDirectory
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	// if the directory is not empty and
        // recursive = false a WebException is thrown
	bool recursive = true;
	ftp.DeleteDirectory("remote_dir", recursive);
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class DeleteDirectory
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	' if the directory is not empty and
        ' recursive = false a WebException is thrown
	Dim recursive As Boolean = True
	ftp.DeleteDirectory("remote_dir", recursive)
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Create remote folder

This example shows how to create a remote folder.

C#

using System;
using KetLabs.FTP;
 
class CreateDirectory
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	if (!ftp.DirectoryExists("remote_dir"))
	{
	   // if the remote folder exists a WebException
 	   // will be thrown
	   ftp.CreateDirectory("remote_dir");
	}
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class CreateDirectory
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	If Not ftp.DirectoryExists("remote_dir") Then
	' if the remote folder exists a WebException
	' will be thrown
		ftp.CreateDirectory("remote_dir")
	End If
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Check is it a folder

This example shows how to check is a remote resource a folder or not.

C#

using System;
using KetLabs.FTP;
 
class IsDirectory
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	bool isDirectory = ftp.IsDirectory("remote_dir");
	Console.WriteLine(isDirectory);
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class IsDirectory
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	Dim isDirectory As Boolean = ftp.IsDirectory("remote_dir")
	Console.WriteLine(isDirectory)
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Get current remote folder

This example demonstrates how to get the full path of the current remote folder

C#

using System;
using KetLabs.FTP;
 
class CurrentDirectory
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	// get the current directory
	string remoteDirectory = ftp.CurrentDirectory;
	Console.WriteLine(remoteDirectory);
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class CurrentDirectory
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	' get the current directory
	Dim remoteDirectory As String = ftp.CurrentDirectory
	Console.WriteLine(remoteDirectory)
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Change the current remote folder

The example below shows how to change the current remote folder. The new folder in the example is relative to the current remote folder. To specify an absolute folder path place the / sign at the start of the path, for example “/newdir”

C#

using System;
using KetLabs.FTP;
 
class ChangeDirectory
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	ftp.ChangeDirectory("remote_dir");
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class ChangeDirectory
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	ftp.ChangeDirectory("remote_dir")
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top


Change the current remote folder one level up

This example demonstrates how to change the current remote folder one level up.

C#

using System;
using KetLabs.FTP;
 
class ChangeDirectoryUp
{
 public static void Demo()
 {
	FtpClient ftp = new FtpClient();
	ftp.Username = Helper.USERNAME;
	ftp.Password = Helper.PASSWORD;
	ftp.Connect(Helper.HOST);
 
	ftp.ChangeDirectoryUp();
 
	ftp.Disconnect();
 }
}

VB.NET

Imports System
Imports KetLabs.FTP
 
Class ChangeDirectoryUp
 Public Shared Sub Demo()
	Dim ftp As New FtpClient()
	ftp.Username = Helper.USERNAME
	ftp.Password = Helper.PASSWORD
	ftp.Connect(Helper.HOST)
 
	ftp.ChangeDirectoryUp()
 
	ftp.Disconnect()
 End Sub
End Class

Back to Top

recursive