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

Recommended Answers

All 2 Replies

A FileInfo class has an Extention property, which might come in handy. Click Here

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.