How to open file that is shown in the listbox

Viewed 27

I created a File Searcher ( I provide the directory of folders in Open Directory button and i choose Extension if (txt or png or html etc...) and in term i write the word i want to search for it like

Example.txt index.html, etc... and after that I click Search For Files and I get Result In the listBox1

After that to Search for a specific word I write in Term The Word To Search it inside a selected txt file from the listbox1 but for txt Files I want to Search a specific Word in all txt files without selecting any file from listbox1 and I want a code to show how many files I get in the result (listbox1) in number and how to open a specific file from listbox1 just with clicking it 2 times and how to copy a file or select or remove from listbox1

Questions :

how to change the code of this (Search For Word In Specific File) in a code that can (Search For Word In All Files And Give me Results)?

How to open file that is shown in the listbox?

How to copy file inside the file in the listbox?

How to copy a file or select or remove from listbox1 ?

How to open a specific file from listbox1 just with clicking it 2 times ?

What is the code to show how many files i get in the result (listbox1) in number ?

This is The Picture Of The Searcher

The Code :

private void FileFound(string path)
{

    listBox1.BeginInvoke((Action)delegate ()
    {
        listBox1.Items.Add(path);
    });
}

private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs args)
{
    MessageBox.Show("Done");
}

private void WorkInBackground(object sender, DoWorkEventArgs args)
{
    searcher.Search();
}


private void button1_Click(object sender, EventArgs e)
{
    if(comboBox1.SelectedIndex == -1 || string.IsNullOrEmpty(textBox1.Text)|| string.IsNullOrEmpty(textBox2.Text) )
    {
        MessageBox.Show("Please Select An Extension Or Select A Directory Or Fill a word");
        return;
    }
    listBox1.Items.Clear();
    this.searcher.Term = textBox2.Text;
    this.searcher.Dir = textBox1.Text;
    this.searcher.Ext = comboBox1.SelectedItem.ToString();
    bgWorker.RunWorkerAsync();
}

private void button2_Click(object sender, EventArgs e)
{
    CommonOpenFileDialog dialog = new CommonOpenFileDialog();
    dialog.IsFolderPicker = true;
    dialog.RestoreDirectory = true;
    if(dialog.ShowDialog() == CommonFileDialogResult.Ok)
    {
        textBox1.Text = dialog.FileName;
    }
}

private void button3_Click(object sender, EventArgs e)
{
    try
    {
        if (!listBox1.SelectedItem.ToString().Contains(".txt") )
        {
            MessageBox.Show("Please Select Text File");
            return;
        }
        if (string.IsNullOrEmpty(textBox2.Text))
        {
            MessageBox.Show("Please type a Term");
            return;
        }
            this.searcher.Term = textBox2.Text;
        this.searcher.Dir = listBox1.SelectedItem.ToString();
        string words = File.ReadAllText(listBox1.SelectedItem.ToString());
        if (words.ToLower().Contains(textBox2.Text.ToLower()))
        {
            MessageBox.Show("word Founded");
        }
        else
        {
            MessageBox.Show("No Such a Word");
        }
    }
    catch (Exception)
    {
        MessageBox.Show("Please Select A File");
    }
   
    //bgWorker.RunWorkerAsync();
}

}

0 Answers
Related