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.

Tags:    

Questions?

Consider using our Q&A forum for asking questions.