简体   繁体   English

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

[英]Trying to return the name of the oldest file on an FTP site in C#

Here's what I have so far: 这是我到目前为止的内容:

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);
}

What this seems to do is return me a formatted string with the details of all the files in the directory. 这似乎是要给我返回一个格式化的字符串,其中包含目录中所有文件的详细信息。 Is it possible to either just get a single file (based on a criteria like creation date) or is there a library that will parse this string for me? 是否可以只获取一个文件(基于创建日期等条件),还是有一个库可以为我解析此字符串?

The FTP protocol defines no method to search for files based on their date, so you will have to work with the list of files you get. FTP协议没有定义基于文件日期搜索文件的方法,因此您将不得不使用获取的文件列表。

Since the string formatting may differ from one ftp-server to another, the safest option would be to use the WebRequestMethods.Ftp.GetDateTimestamp method for each file. 由于一台ftp服务器的字符串格式可能有所不同,因此最安全的选择是对每个文件使用WebRequestMethods.Ftp.GetDateTimestamp方法。

This will require multiple roundtrips of course which may or may not be acceptable in your case. 当然,这将需要多次往返,对于您而言,这可能会接受也可能无法接受。

If you need to parse the List output, the Indy project might help, since it can parse practically all known ftp output formats. 如果您需要解析List输出,则Indy项目可能会有所帮助,因为它可以解析几乎所有已知的ftp输出格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM