0 votes

How I can list all files using Limilabs FTP library in a root folder for example ?
I would like verify if exist files in a folder before.

I tried this way?

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

     client.AuthTLS();

     client.Login("username", "password");

     RemoteSearchOptions options = new RemoteSearchOptions("*.txt", true);
     List<RemoteSearchItem> items = client.Search(options);

     foreach (RemoteSearchItem item in items)
     {
         string remotePath = item.GetRemotePath(); // full path e.g.: folder/a.txt
         string name = item.FtpItem.Name; // name e.g.: a.txt
         bool isFolder = item.IsFolder;
     }
     client.Close();
}

Happen a error when execute line 11. follow the error : Cannot allocate local port.

but is very stranger, because I can do this:

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();
}
by (470 points)

1 Answer

+1 vote

You can use Ftp.FileExists method to check if specified file exists on FTP server:

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

    client.AuthTLS();

    client.Login("username", "password");


    client.Upload("FileExists.txt", new byte[] {1, 2, 3});

    bool exists = client.FileExists("FileExists.txt");
    bool doesNotExist = client.FileExists("NoSuchFile.txt");

    client.DeleteFile("FileExists.txt");

    client.Close();

    Assert.IsTrue(exists);
    Assert.IsFalse(doesNotExist);
}

"Cannot allocate local port" is most likely an error returned by your server. Search needs to traverse all folders and issue several commands, it seems your FTP server doesn't like it.

You can always turn on logging to see raw client server communication.

by (301k points)
Well,

I would like know if exists any file in root folder.

for example :   bool exists = client.FileExists("*.txt");

is not possible this way.
You are correct. You can't use patterns in such way. Consider using GetList or use Search with RemoteSearchOptions.Recursive set to false.
Thank you. this dll is fantastic.
...