某些 FTP 服务器未能包含隐藏文件以响应 LIST 和 NLST 命令(位于 ListDirectoryDetails 和 ListDirectory 后面)。
一种解决方案是使用MLSD 命令,FTP 服务器会向该命令返回隐藏文件。不管怎样,MLSD 命令是与 FTP 服务器通信的唯一正确方式,因为它的响应格式是标准化的(LIST 不是这种情况)。
但是.NET framework/FtpWebRequest不支持MLSD命令。
为此,您必须使用不同的第 3 方 FTP 库。
例如 WinSCP .NET assembly 你可以使用:
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Ftp,
HostName = "ftp.example.com",
UserName = "user",
Password = "mypassword",
};
using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);
RemoteDirectoryInfo directory = session.ListDirectory("/remote/path");
foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine(
"{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions,
fileInfo.LastWriteTime);
}
}
请参阅Session.ListDirectory method 的文档。
如果服务器支持,WinSCP 将使用MLSD。如果没有,它将尝试使用-a 技巧(如下所述)。
(我是 WinSCP 的作者)
如果你被FtpWebRequest卡住了,你可以尝试使用-a开关和LIST/NLST命令。虽然这不是任何标准开关(FTP 中没有开关),但许多 FTP 服务器确实可以识别它。它使它们返回隐藏文件。
要欺骗FtpWebRequest 将-a 开关添加到LIST/NLST 命令,请将其添加到 URL:
WebRequest.Create("ftp://ftp.example.com/remote/path/ -a");