How to order files received from FTP by creation date in C #?

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
        {
            /* Create an FTP Request */
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(URI);

            /* Log in to the FTP Server with the User Name and Password Provided */
            ftpRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);

            /* When in doubt, use these options */
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;

            /* Specify the Type of FTP Request */
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            /* Establish Return Communication with the FTP Server */
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

            /* Establish Return Communication with the FTP Server */
            ftpStream = ftpResponse.GetResponseStream();

            /* Get the FTP Server Response Stream */
            StreamReader ftpReader = new StreamReader(ftpStream);

            /* Store the Raw Response */
            string directoryRaw = null;

            /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
            try
            {
                while (ftpReader.Peek() != -1) 
                { 
                    directoryRaw += ftpReader.ReadLine() + "|"; 
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            /* Resource Cleanup */
            ftpReader.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;

            /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
            try
            {
                string[] directoryList = directoryRaw.Split("|".ToCharArray()); 

                return directoryList;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        /* Return an Empty string Array if an Exception Occurs */
        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?

+4
2

, , , , . Array.Sort(arrayOfFiles)

.

try
        {
            /* Create an FTP Request */
            ftpRequest = (FtpWebRequest)FtpWebRequest.Create(URI);

            /* Log in to the FTP Server with the User Name and Password Provided */
            ftpRequest.Credentials = new NetworkCredential(ftpUsername, ftpPassword);

            /* When in doubt, use these options */
            ftpRequest.UseBinary = true;
            ftpRequest.UsePassive = true;
            ftpRequest.KeepAlive = true;

            /* Specify the Type of FTP Request */
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;

            /* Establish Return Communication with the FTP Server */
            ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();

            /* Establish Return Communication with the FTP Server */
            ftpStream = ftpResponse.GetResponseStream();

            /* Get the FTP Server Response Stream */
            StreamReader ftpReader = new StreamReader(ftpStream);

            /* Store the Raw Response */
            string directoryRaw = null;

            /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
            try
            {
                while (ftpReader.Peek() != -1) 
                { 
                    directoryRaw += ftpReader.ReadLine() + "|"; 
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }

            /* Resource Cleanup */
            ftpReader.Close();
            ftpStream.Close();
            ftpResponse.Close();
            ftpRequest = null;

            /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
            try
            {
                string[] directoryList = directoryRaw.Split("|".ToCharArray());
                Array.Sort(directoryList);

                return directoryList;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        /* Return an Empty string Array if an Exception Occurs */
        return new string[] { "" };
+1

- ?

 string[] fileNames = Directory.GetFiles("directory ", "*.*");

 DateTime[] creationTimes = new DateTime[fileNames.Length];

 for (int i = 0; i < fileNames.Length; i++)

 creationTimes[i] = new FileInfo(fileNames[i]).CreationTime;

 Array.Sort(creationTimes, fileNames);
-1

Source: https://habr.com/ru/post/1620283/


All Articles