0

I am using following code which is displaying all files in listbox. But only files with specified extension selected from combobox(i.e .txt or .html....) should be displayed

 private void button1_Click(object sender, EventArgs e)
        {
            string search = textBox1.Text;
            string folder = textBox2.Text;

            MessageBox.Show(comboBox1);
            string[] allFiles = System.IO.Directory.GetFiles(folder, "comboBox1");//Change path to yours

            foreach (string file in allFiles)
            {
               // MessageBox.Show(allFiles[0]);

               var word = File.ReadAllText(file);
                //Console.WriteLine(word);
                //Console.ReadKey();
                if (word.Contains(search))
                {
                    listBox1.Items.Add("Match Found : " + file);
                    //MessageBox.Show("Match Found : " + file);
                }
                else
                    listBox1.Items.Add("Match NOT Found : " + file);
            }
3
Contributors
2
Replies
17
Views
3 Years
Discussion Span
Last Post by Fenrir()
0

Here's a simple example that populates the combobox with all the file extensions in your folder.

string[] Files = System.IO.Directory.GetFiles(folder);

foreach (string file in Files)
{
  FileInfo File = new FileInfo(file);

  if (!comboBox1.Items.Contains(File.Extension))
  {
    comboBox1.Items.Add(File.Extension);
  }              
}

You should be able to take it from here.

Edited by Fenrir()

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.