4

I recive a list of filenames from a FTP directory. But as it is now the filenames are order by their filename. What i want to is to order the files by the creation date before i save them in the List. I just can't figure out how to?

Here is how i recive the filenames and add them to a 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:

So i figured out that the way i recived the files before does not contain the details on when the files was created, so i needed to get the files in another way for me to get the creation date.
Here is the new way that i get the 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's 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't quite figure out how to sort the files after the creation date. Is there a way of writing a linq query, like Orderby, of some sort?

Code_Viking
  • 618
  • 1
  • 8
  • 26
  • 1
    This SO [answer](http://stackoverflow.com/questions/4454281/retrieving-creation-date-of-file-ftp#7574308) might hold the key. – kayess Dec 16 '15 at 08:41

2 Answers2

1

So after I found out that I needed to retrieve a detailed list of the files the sort problem was easy to solve. I just needed to call

Array.Sort(arrayOfFiles)

Here is the working code:

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's 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[] { "" };
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Code_Viking
  • 618
  • 1
  • 8
  • 26
-2

Would something like this work for you?

 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);
musab shaheed
  • 150
  • 1
  • 7