Search code examples
c#ftpsfluentftp

FluentFTP GetListing doesn't return any result


I seem to be unable to get any listing from / of the FTP server. (FileZilla is showing the directories and files).

I got this code:

                FtpClient ftpConn = new FtpClient();
                ftpConn.Host = FtpServer;
                ftpConn.Port = FtpPort;
                ftpConn.Credentials = new System.Net.NetworkCredential(Username, Password);
                ftpConn.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
                ftpConn.EncryptionMode = FtpEncryptionMode.Implicit;
                ftpConn.ValidateCertificate += new FtpSslValidation(Client_ValidateCertificate);
                ftpConn.BulkListing = false;
                //ftpConn.DataConnectionType = FtpDataConnectionType.AutoPassive;
                ftpConn.Connect();

                FtpListItem[] FtpFolders = null;

                FtpFolders = ftpConn.GetListing(Folder);

But it doesn't work. I tried the FTP options but didn't get any result. Any more suggestions?


Solution

  • Based on your code,
    You can't get any result because of fail to connect to FTP server.

    Here is what you missed, refer to this: FAQ section of FluentFTP.
    You may have certificate(*.crt, *.cer) file, bring it into your source code as below.

    ftpConn.ClientCertificates.Add(new X509Certificate2(@"C:\ftpServer.crt"));
    

    If your certificate file doesn't have root chain. (for example, made by your self or in case of it is private cert file).
    You need to add more specific code at,

    ftpConn.ValidateCertificate += new FtpSslValidation(Client_ValidateCertificate);

    private void Client_ValidateCertificate(FtpClient control, FtpSslValidationEventArgs e)
    {            
        if (e.PolicyErrors == SslPolicyErrors.None || e.Certificate.GetRawCertDataString() == "Use this condition for your situation")
        {
            e.Accept = true;
        }
        else
        {
            if (e.PolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors)
            {
                //In this case, you need to choose connect or not. If your certificate file doen't have root chain.
            }
            else
            {
                //throw new Exception($"{e.PolicyErrors}{Environment.NewLine}{GetCertificateDetails(e.Certificate)}");
            }
        }
    }
    

    PS : If your FTP service on Windows, you don't have any choice but if it work on linux or unix, You can use SFTP with "Renci.SshNet.

    UPDATED : Now windows is support to openSSH, so we can use sftp. Installation of OpenSSH For Windows Server 2019 and Windows 10