hi,
i am developing a desktop application in VS 2008 standard edition,
i have a form with a text box with a button and a grid view. when i type a part of the file name in the text box and then click on the button all the files which has that text name should be displayed with the location and the link to that file. when i click the link the file should be opened. how can i do this , please help me, i am new to C#.NET and not familiar with this language specially for these things.

can this be done by the Directory searcher, if can how??

Recommended Answers

All 11 Replies

well... to find all the files you can use Directory class and GetFiles(), GetDirectories() method... below I've pasted the code snippet showing how it can be used... it seems to work but I did that in the hurry so you can find there some mistakes...
btw I added the comboBox to give the method the start point and paste some information to richtextbox:

// here I add all logical drive to combobox
        private void Form1_Load(object sender, EventArgs e)
        {
            string[] logicalDrives = Directory.GetLogicalDrives();
            for (int i = 0; i < logicalDrives.Length; i++)
            { 
                cbDirectorySearcher.Items.Add(logicalDrives[i]);
            }
        }
       // recursive function
        private void checkFiles(string pattern, string path)
        {
            string[] directories = Directory.GetDirectories(path);
            string[] files = Directory.GetFiles(path);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Contains(pattern))
                    rtbDirectories.Text += files[i].Substring(files[i].LastIndexOf('\\') + 1) + "\t" + files[i] + "\n";
            }
            for (int i = 0; i < directories.Length; i++)
            {
                checkFiles(pattern, directories[i]);
            }
        }
        // main method - in here I call recursive function
        private void button10_Click(object sender, EventArgs e)
        {
            string pattern = tbPattern.Text;
            if (pattern == String.Empty)
                return;
            string path = cbDirectorySearcher.SelectedItem.ToString();
            checkFiles(pattern, path);
            MessageBox.Show("Finished");
        }

btw searching the files can take a lot of time... so I recommend put it to another thread...

hey, there are some errors underlining some words, how do i overcome that??? the ones which are bold are giving and error. can u please help me with this?

C# Syntax

private void Form1_Load(object sender, EventArgs e)
        {
            string[] logicalDrives = [B]Directory[/B].GetLogicalDrives();
            for (int i = 0; i < logicalDrives.Length; i++)
            {
                [B]cbDirectorySearcher[/B].Items.Add(logicalDrives[i]);
            }
        }
 private void button1_Click(object sender, EventArgs e)
        {
            string pattern = [B]tbPattern[/B].Text;
            if (pattern == String.Empty)
                return;
            string path = [B]cbDirectorySearcher[/B].SelectedItem.ToString();
            checkFiles(pattern, path);
            MessageBox.Show("Finished");
        }
        
// recursive function
        private void checkFiles(string pattern, string path)
        {
            string[] directories = [B]Directory[/B].GetDirectories(path);
            string[] files = [B]Directory[/B].GetFiles(path);
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Contains(pattern))
                   [B] rtbDirectories[/B].Text += files[i].Substring(files[i].LastIndexOf('\\') + 1) + "\t" + files[i] + "\n";
            }
            for (int i = 0; i < directories.Length; i++)
            {
                checkFiles(pattern, directories[i]);
            }
        }

please help me with this??
and also what do i have to post in another thread??

thanxxx

There is a nice properties that you can use that will make everything way more easy.

this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
                        this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.FileSystemDirectories;

Not as fancy, but it does the job :) You can also look for these properties in the designer to see the options :)

pierlucSS:
what is this reagrding. in which line do i have to replace in the original post????????????

pierlucSS:
what is this reagrding. in which line do i have to replace in the original post????????????

In your designer.cs or simply edit the properties in the [Design]

ok...
1. there is a good practice to always name the components added to the form to reflect their functions (= what they were added for)... and there are some conventions of naming the components... I use the one which suggest to give at the beginning the abbreviated name of the component and then something that suggest what the component do... so:
cbDirectorySearcher = cb (for ComboBox) and DirectorySearcher (as in that combo box I want to specify the directory from which I will start searching for the files)
tbPattern = tb (for TextBox) and Pattern (as in that text box I want the user to provide the name of the file or part of it)
rtbDirectories it should probably be called rtbFound or sth like that... rtb is for richTextBox... in here I add the results...
2. Directory is a class from System.IO namespace... so add
using System.IO...
3. if it comes to what PierlucSS suggested, in the example that I've provided above you can apply those properties to combobox... it can be set in Properties window for combobox and it will help the user specify the directory from which to start searching...

hey thanks a lot Szpilona,
by the way how can i open for a dialog box and select the path that i want the file to be searched???

how can i do this??

hey thanks a lot Szpilona,
by the way how can i open for a dialog box and select the path that i want the file to be searched???

how can i do this??

FolderBrowserDialog dlg = new FolderBrowserDialog();
if(dlg.ShowDialog() == DialogResult.OK)
{
        //do whatever with dlg.SelectedPath
}

you can also add the button and OpenFileDialog control from toolbox... change the text property of the button to "...", add button click event and paste the code (change only the names of the controls):

private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.ShowDialog();
            textBox1.Text = openFileDialog1.FileName;
        }

oh k

so how can i apply to the code u have posted??
thanx

sorry I've made a mistake... read your question a little bit too fast... you want to select a folder not a file so as PierlucSS suggested you have to use FolderBrowserDialog... you can link this with the rest of your code in many different ways... I usually accomplish that by adding the dialog and button from toolbox, putting the button by the right side of the control which will be provided with results from the dialog, changing the text property of the button to "..." and adding the:

private void button11_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
                cbDirectorySearcher.Text = folderBrowserDialog1.SelectedPath;
        }

many programs use that pattern... but as I've said it is not the only way...

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.