Click here to Skip to main content
16,008,075 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi friends,

Here i have a problem, which is how to list the contents(files) of a search folder in listbox. The issue is i have many folder, each folder have many files with format .txt. I need, if i type the folder name in textbox and click the button, in the listbox shows all the files inside the search folder. In below i add my code, but it not works. Please help me....

C#
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 + "*" + "*.*");
               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>");
           }
       }
Posted
Updated 2-Nov-15 14:48pm
v3
Comments
Garth J Lancaster 2-Nov-15 20:47pm    
why are you posting this again ? iirc you were being helped on the same or similar question ~15 hours ago - what was wrong with those answers ?
CgKumar 2-Nov-15 21:25pm    
That is not same question as this. Were i post 15 hours ago is about to list the search file according to the date modified...
ZurdoDev 2-Nov-15 21:16pm    
Where are you stuck?
CgKumar 2-Nov-15 21:52pm    
i don't know whether my code correct or wrong... the problem is no files are listed at listbox if i search...please help me...

1 solution

Hi Friends, i m settle ready the problem. i do some mistake in code. By the way thanks friends for the help. The modified code is:

string[] files = Directory.GetFiles(@"\\192.123.1.16\Report\Result\"+ search, "*.txt", SearchOption.AllDirectories);


Regards,
Thanes
 
Share this answer
 

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