仅 FtpWebRequest return 个带有 ListDirectoryDe​​tails 的文件名

FtpWebRequest only return file names with ListDirectoryDetails

我发现一个代码非常有用,但是下面的代码 return 来自一台 FTP 服务器的目录和文件的名称,我只需要获取文件的名称。

ftpRequest = (FtpWebRequest) FtpWebRequest.Create(host + "/" + directory);
/* Log in to the FTP Server with the User Name and Password Provided */
ftpRequest.Credentials = new NetworkCredential(user, pass);
/* When in doubt, use these options */
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
/* Specify the Type of FTP Request */
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
/* Establish Return Communication with the FTP Server */
ftpResponse = (FtpWebResponse) ftpRequest.GetResponse();
/* Establish Return Communication with the FTP Server */
ftpStream = ftpResponse.GetResponseStream();
/* Get the FTP Server's Response Stream */
StreamReader ftpReader = new StreamReader(ftpStream);
/* Store the Raw Response */
string directoryRaw = null;
/* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
try{
     while (ftpReader.Peek() != -1){
        directoryRaw += ftpReader.ReadLine() + "|";
     }
 }
 catch(Exception ex)
 {
      //Do something
 }
...
...
...

我调查了一下,但是 WebRequestMethods.Ftp 只有 ListDirectoryListDirectoryDetails,都是 return 目录和文件的名称:(..

有人可以帮助我..

谢谢

ListDirectory 向仅 returns 文件名的服务器发出 NLST 命令。

ListDirectoryDetails 向服务器发出 LIST 命令,通常 returns 文件名和详细信息。

但这最终取决于服务器,它是什么returns。如果它 returns 文件名仅适用于两者,则 FtpWebRequest 对此无能为力。

服务器可能支持 MLSD 命令到 returns 文件详细信息,但 FtpWebRequest 不支持。另一种替代方法是对每个文件分别使用 GetFileSizeGetDateTimestamp。但这很慢。

另请参阅我在 Retrieving creation date of file (FTP) 的回答。

获取ftp对象名称的方法总是return完整列表(目录和文件)。我通过扩展名限制名称,例如

var xmlFiles = files.FindAll( foo => foo.ToUpper().Contains( ".XML" )|| foo.ToUpper().Contains( ".TXT" ));