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.
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.