尝试在C#中返回FTP站点上最旧文件的名称

时间:2011-01-31 13:48:28

标签: c# .net-3.5 ftp

这是我到目前为止所拥有的:

FtpWebRequest reqFTP;
string returnString = "";

Uri serverFile = new Uri(ftpServer + "/" + dirName);

reqFTP = (FtpWebRequest)FtpWebRequest.Create(serverFile);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUser, ftpPass);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();

Stream ftpStream = response.GetResponseStream();
long cl = response.ContentLength;
int bufferSize = 2048;
int readCount;
byte[] buffer = new byte[bufferSize];

readCount = ftpStream.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
    returnString += System.Text.ASCIIEncoding.ASCII.GetString(buffer);
    readCount = ftpStream.Read(buffer, 0, bufferSize);
}

这似乎是给我一个格式化的字符串,其中包含目录中所有文件的详细信息。是否可以只获取单个文件(基于创建日期等标准),或者是否有可以为我解析此字符串的库?

1 个答案:

答案 0 :(得分:1)

FTP协议没有定义根据日期搜索文件的方法,因此您必须使用所获得的文件列表。

由于字符串格式可能因ftp-server而异,因此最安全的选择是对每个文件使用WebRequestMethods.Ftp.GetDateTimestamp方法。

这需要多次往返,在您的情况下可能会或可能不会接受。

如果你需要解析List输出,Indy project可能会有所帮助,因为它几乎可以解析所有已知的ftp输出格式。