I get a list of file names from the FTP directory. But since now the file names are sorted by their name. I want to order files by creation date before I save them in a list. I just can't figure out how to do this?
This is how I get the file names and add them to the list of strings.
try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(URI);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
string names = reader.ReadToEnd();
reader.Close();
response.Close();
return names.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries).ToList();
}
catch (Exception)
{
throw;
}
EDIT:
Therefore, I realized that the way I received the files before that does not contain details about when the files were created, so I needed to get the files differently for me to get the creation date.
Here is a new way to get files.
try
{
ftpRequest = (FtpWebRequest)FtpWebRequest.Create(URI);
ftpRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);
ftpRequest.UseBinary = true;
ftpRequest.UsePassive = true;
ftpRequest.KeepAlive = true;
ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
ftpStream = ftpResponse.GetResponseStream();
StreamReader ftpReader = new StreamReader(ftpStream);
string directoryRaw = null;
try
{
while (ftpReader.Peek() != -1)
{
directoryRaw += ftpReader.ReadLine() + "|";
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
ftpReader.Close();
ftpStream.Close();
ftpResponse.Close();
ftpRequest = null;
try
{
string[] directoryList = directoryRaw.Split("|".ToCharArray());
return directoryList;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
return new string[] { "" };
But I still can not figure out how to sort the files after the creation date. Is there a way to write a linq query like Orderby?