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);
            }

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 3 Years Ago by Fenrir()

This article has been dead for over six months. Start a new discussion instead.