none
How to list files in listbox according to the date modified of the file? RRS feed

  • Question

  • Hi friends, i m do C# coding for list files in listbox according to the date modified of the file. But in the code shows some error. Please give me suggestion how to settle the error.

    protected void Button1_Click(object sender, EventArgs e)
            {
                ListBox1.Items.Clear();
                string search = TextBox1.Text; // here type the folder name
                if (search != "")
                {
                    string[] ext = new string[] { ".jpg", ".gif", ".png",".PNG", ".GIF", ".JPG", ".bmp", ".BMP" };
                    var directory = new DirectoryInfo(@"\\192.168.1.20\mark\test\");
                    var files = Directory.GetFiles(directory + search, "*.*", SearchOption.AllDirectories).Where(s => ext.Any(n => s.EndsWith(n)));
                    
                    DateTime[] creationtime = new DateTime[files.Length];
                    for (int i = 0; i < files.Length; i++)
                        creationtime[i] = new FileInfo(files[i]).CreationTime;
                    Array.Sort(creationtime, files);
    
                        foreach (string file in files)
                        {
                            ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file)); // listed all files in the search folder
                        }
                    {
                        search = "";
                    }
                }
                else
                {
                    Response.Write("<script>alert('Please Enter Search Keyword');</script>");
                }
            }

    Error over: (the word italic shows error)

     DateTime[] creationtime = new DateTime[files.Length];
                    for (int i = 0; i < files.Length; i++)
                        creationtime[i] = new FileInfo(files[i]).CreationTime;
                    Array.Sort(creationtime, files);

    Please help me....

    Wednesday, November 4, 2015 3:52 AM

Answers

  • Error over: (the word italic shows error)
     DateTime[] creationtime = new DateTime[files.Length];
                    for (int i = 0; i < files.Length; i++)
                        creationtime[i] = new FileInfo(files[i]).CreationTime;

    For these lines try these instead:

    //DateTime[] creationtime = new DateTime[files.Length];
    DateTime[] creationtime = new DateTime[files.ToArray().Length];
    
                    //for (int i = 0; i < files.Length; i++)
                    //  creationtime[i] = new FileInfo(files[i]).CreationTime;
                    for (int i = 0; i < files.ToArray().Length; i++)
                        creationtime[i] = new FileInfo(files.ToArray()[i]).CreationTime;
    


    - Wayne

    Wednesday, November 4, 2015 5:18 AM
  • Hi Wayne, Thanks for the code. It is working. But the problem is the file is not arrange according to the date modified.


    Because it is the temporary array of files that gets sorted. Try this:

    string[] sarray = files.ToArray();
    //Array.Sort(creationtime, files.ToArray());
    Array.Sort(creationtime, sarray);
    
    //foreach (string file in files)
    foreach (string file in sarray)
    

    - Wayne

    Wednesday, November 4, 2015 6:02 AM
  • i have another extra question, use the same program list the files from latest date modified to oldest. Because the previous program is list the files from oldest to latest date modified.

    Something similar to this should work. (It's a first thought, so it probably can be refined.)

    public class myReverserClass : IComparer
    {
        // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        int IComparer.Compare(Object x, Object y)
        {
            return ((new CaseInsensitiveComparer()).Compare(y, x));
        }
    }
    
    ...
    
    IComparer myComparer = new myReverserClass();
    
    string[] sarray = files.ToArray();
    Array.Sort(creationtime, sarray, myComparer);
    
    foreach (string file in sarray)
        {
    ...

    - Wayne


    Wednesday, November 4, 2015 6:31 AM

All replies

  • Error over: (the word italic shows error)
     DateTime[] creationtime = new DateTime[files.Length];
                    for (int i = 0; i < files.Length; i++)
                        creationtime[i] = new FileInfo(files[i]).CreationTime;

    For these lines try these instead:

    //DateTime[] creationtime = new DateTime[files.Length];
    DateTime[] creationtime = new DateTime[files.ToArray().Length];
    
                    //for (int i = 0; i < files.Length; i++)
                    //  creationtime[i] = new FileInfo(files[i]).CreationTime;
                    for (int i = 0; i < files.ToArray().Length; i++)
                        creationtime[i] = new FileInfo(files.ToArray()[i]).CreationTime;
    


    - Wayne

    Wednesday, November 4, 2015 5:18 AM
  • Error over: (the word italic shows error)
                    Array.Sort(creationtime, files);

    Similarly, try this:

    Array.Sort(creationtime, files.ToArray());

    - Wayne

    Wednesday, November 4, 2015 5:25 AM
  • Hi Wayne, Thanks for the code. It is working. But the problem is the file is not arrange according to the date modified. Can you please help me...
    Wednesday, November 4, 2015 5:50 AM
  • Hi Wayne, Thanks for the code. It is working. But the problem is the file is not arrange according to the date modified.


    Because it is the temporary array of files that gets sorted. Try this:

    string[] sarray = files.ToArray();
    //Array.Sort(creationtime, files.ToArray());
    Array.Sort(creationtime, sarray);
    
    //foreach (string file in files)
    foreach (string file in sarray)
    

    - Wayne

    Wednesday, November 4, 2015 6:02 AM
  • Thank You Wayne. It is working. Wayne i have another extra question, use the same program list the files from latest date modified to oldest. Because the previous program is list the files from oldest to latest date modified. Please help me...
    Wednesday, November 4, 2015 6:09 AM
  • i have another extra question, use the same program list the files from latest date modified to oldest. Because the previous program is list the files from oldest to latest date modified.

    Something similar to this should work. (It's a first thought, so it probably can be refined.)

    public class myReverserClass : IComparer
    {
        // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
        int IComparer.Compare(Object x, Object y)
        {
            return ((new CaseInsensitiveComparer()).Compare(y, x));
        }
    }
    
    ...
    
    IComparer myComparer = new myReverserClass();
    
    string[] sarray = files.ToArray();
    Array.Sort(creationtime, sarray, myComparer);
    
    foreach (string file in sarray)
        {
    ...

    - Wayne


    Wednesday, November 4, 2015 6:31 AM
  • Hi Wayne. Thanks for the code. Wayne at "IComparer" and "CaseInsensitiveComparer" is shows error. Could i need to add any related references...? 
    Wednesday, November 4, 2015 6:39 AM
  • Wayne at "IComparer" and "CaseInsensitiveComparer" is shows error. Could i need to add any related references...? 

    using System.Collections;

    - Wayne

    Wednesday, November 4, 2015 6:43 AM
  • Thank You my friend. You are really genius. It is working fine....:)
    Wednesday, November 4, 2015 6:46 AM
  • Hi cgthanes08,

    Please remember to close this thread by marking WayneAKing's reply as an answer.

    Best regards,

    Krsitin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, November 5, 2015 6:30 AM
    Moderator