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.
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.
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.
精彩评论