简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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