vbnet3d
vbnet3d

Reputation: 1151

Files exist on server but are not returned by list directory command

I am using the WinSCP .NET NuGet package to access a third-party FTP server over which I have no control.

Problem: It seems that, after the initial LIST command (in which all the files are listed), the server no longer lists the files that are present in the directory - only the sub-directorires. However, if I open the same FTP directory in FireFox or FileZilla, the files show up. By contrast, the WinSCP tool itself does not list the files.

It appears that WinSCP is obeying a directive from the server to not list these files. The built-in .NET FTP library (FtpWebRequest) has exactly the same limitation.

The code I am using to access the server is below:

public class FTP
{
    private SessionOptions opts = new SessionOptions();

    public FTP(string _userName, string _password, string _server)
    {
        opts.UserName = _userName;
        opts.Password= _password;
        opts.HostName = _server;
        opts.Protocol = Protocol.Ftp;
        opts.FtpMode = FtpMode.Passive;       
    }        

    public string[] GetFiles(string directory)
    {
        try
        {
            using (Session session = new Session())
            {
                session.Open(opts);

                RemoteDirectoryInfo dir = session.ListDirectory(directory);
                return dir.Files.Where(x => !x.IsDirectory).Select(x => x.Name).ToArray();
            }
        }
        catch (Exception)
        {
            throw;
        }
    }
}

Is there a work-around within WinSCP (or alternatively, another C# library) that can correctly handle this situation?

An external process, such as PowerShell or batch is acceptable, but not ideal.

Upvotes: 1

Views: 1310

Answers (2)

Martin Prikryl
Martin Prikryl

Reputation: 202534

With WinSCP .NET assembly, you have to set FtpListAll option to on/0:

opts.AddRawSettings("FtpListAll", "on");

Or you can have WinSCP GUI generate the code template for you.


With FtpWebRequest, add -a to the URL.

See FtpWebRequest ListDirectory does not return hidden files.

Upvotes: 3

vbnet3d
vbnet3d

Reputation: 1151

It looks like FluentFTP is able to handle this issue with the listing option set to FtpListOption.AllFiles.

I used the following code and successfully listed the files:

FtpListItem[] files = ftp.GetListing(path, FtpListOption.AllFiles)
                .Where(x => x.Type == FtpFileSystemObjectType.File)
                .OrderBy(x => x.Modified)
                .ToArray();

This library also supported the downloads as well.

UPDATE: Per the accepted answer, WinSCP can handle this correctly with ftpListAll set to 0..

Upvotes: 2

Related Questions