1

Here i have a problem with to list the search files into the listbox according to the date modified. The below code is shows only list the search files into the listbox. Could anyone help me how settle this problem please.....

protected void Button1_Click(object sender, EventArgs e)
{
    ListBox1.Items.Clear();
    string search = TextBox1.Text; // here type the folder name
    if (search != "")
    //DirectoryInfo d = new DirectoryInfo(@"\\192.123.1.18\Report\Result" + search);
    {
        string[] files = Directory.GetFiles(@"\\192.123.1.16\Report\Result\"+ search, "*.txt", SearchOption.AllDirectories);
        foreach (string file in files)
        {
            //ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file));
            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>");
    }
}
T.Kumar
  • 23
  • 7

2 Answers2

1

For every file you can call: File.GetLastWriteTime and after that you sort this file list according to last write datetime.

Follow below article for more information. https://msdn.microsoft.com/en-us/library/d5da1572.aspx

  1. First create one class with name FileModifiedDate
  2. Add to properties in this 1.Filename , 2.ModifiedDate and 3.File.

    List<FileModifiedDate> FileList=new List<FileModifiedDate>();
    foreach (string file in files)
    {
        //ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file));
       // ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file)); //
       FileModifiedDate FileInfo=new FileModifiedDate();
       FileInfo.FileName=Path.GetFileName(file);
       FileInfo.File=file;
       FileInfo.ModifiedDate=File.GetLastWriteTime(path);
       FileList.Add(FileInfo);
    }
    FileList=FileList.OrderByDescending(a=>a.ModifiedDate).ToList();
    foreach (FileModifiedDate SingleFile in FileList)
    {
        //ListBox1.Items.Add(new ListItem(Path.GetFileName(file), file));
       ListBox1.Items.Add(new ListItem(SingleFile.FileName, SingleFile.file)); //
    }
    
Kaushik Maheta
  • 1,741
  • 1
  • 18
  • 27
  • Hi friend, can you give me a idea how could i or where could i need to add the "File.GetLastWriteTime"....because i m add the code before the code line "ListBox1.Items.Add...". But the probelem is, it is not work...can you help me please... – T.Kumar Nov 03 '15 at 05:52
  • Hi friend, i got error at FileModifiedDate....it is a class...or need to add any references...please help me.... – T.Kumar Nov 03 '15 at 06:16
  • it is a class you need to create with three preperties FileName,File and ModifiedDate – Kaushik Maheta Nov 03 '15 at 06:17
  • OrderByDescending(a=>a.ModifiedDate).ToList(); – Kaushik Maheta Nov 03 '15 at 06:20
  • Hi Friend, now the line "FileList.OrderByDescending(a => a.ModifiedDate)" is became error...please help me friend... – T.Kumar Nov 03 '15 at 06:25
  • Friend, i m do no how to doing the class...can you give me a sample code for that....please help me.... – T.Kumar Nov 03 '15 at 06:44
0

For every file you can call: FileInfo.LastWriteTimeUtc and after that, you should sort this file list according to their last write DateTime. DateTime class implements compasion operators so that you won't have trouble while sorting

Community
  • 1
  • 1
fatihk
  • 7,789
  • 1
  • 26
  • 48