【问题标题】:FtpWebRequest only return file names with ListDirectoryDetailsFtpWebRequest 只返回带有 ListDirectoryDe​​tails 的文件名
【发布时间】:2016-03-07 10:15:49
【问题描述】:

我发现一个代码非常有用,但是下面的代码从一个 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,都返回目录和文件的名称:(..

有人可以帮助我..

谢谢

【问题讨论】:

标签: c# ftp ftpwebrequest


【解决方案1】:

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

ListDirectoryDetails 向服务器发出 LIST 命令,该命令通常返回带有详细信息的文件名。

但最终取决于服务器,它返回什么。如果它只返回两者的文件名,FtpWebRequest 将无能为力。

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这可能是对我的回答有用的评论。但对于“仅返回带有 ListDirectoryDe​​tails 的文件名” 的问题,这绝对不是一个独立答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2013-07-02
    相关资源
    最近更新 更多