Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Friends,

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.....

C#
protected void Button3_Click(object sender, EventArgs e)
        {
                ListBox2.Items.Clear();

                string search = TextBox1.Text;
                if (search != "")
                {
var result = Directory.GetFiles(@"\\192.126.8.118\test\exam\Result", "*" + search + "*.txt").Union(Directory.GetFiles(@"\\192.126.8.118\test\exam\Result", "*" + search + "*.csv"));
                    foreach (string file in result)
                    {
                        ListBox2.Items.Add(new ListItem(Path.GetFileName(file), file));
                    }
                    {
                        search = "";
                    }
                }
                else
                {
                    Response.Write("<script>alert('Please Enter Search Keyword');</script>");
                }
                //if(ListBox2.Items.Count!=0)
                //{
                //    Response.Write("<script>alert('Search Found');</script>");
                //}
                if (ListBox2.Items.Count == 0)
                {
                    Response.Write("<script>alert('Sorry!! SearchNot Found');</script>");
                }


        }
Posted
Comments
VR Karthikeyan 1-Nov-15 22:55pm    
What is your problem or requirement?, please explain it clearly.
CgKumar 2-Nov-15 0:26am    
Hi, I need the listbox should list the files according to the date modified. Please help me....

1 solution

C#
string path = @"D:\bk";
string searchPattern = "*.txt";

DirectoryInfo di = new DirectoryInfo(path);
DirectoryInfo[] directories =
    di.GetDirectories(searchPattern, SearchOption.AllDirectories);

var result =
    di.GetFiles(searchPattern, SearchOption.AllDirectories).OrderBy(f => f.LastWriteTime);
foreach (FileInfo file in result)
{
     ListBox2.Items.Add(new ListItem(Path.GetFileName(file.FullName), file.FullName));
}
 
Share this answer
 
v3
Comments
CgKumar 2-Nov-15 0:35am    
Hi, it shows error at "ListBox2.Items.Add(new ListItem(file.FullName), file.FullName);"
The error mention "No overload for method 'Add' takes 2 arguments". Please help me....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900