Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Scan All File in Folder and Subfolder? [duplicate]

Tags:

c#

Possible Duplicate:
How to recursively list all the files in a directory in C#?

How to scan all file in Folder and Subfolder?

Here is the code I have:

private void button1_Click(object sender, EventArgs e)
{
    folderBrowserDialog1.ShowDialog();
    label2.Text = folderBrowserDialog1.SelectedPath;
    viruses = 0;
    progressBar1.Value = 0;
    label1.Text+= viruses.ToString();
    listBox1.Items.Clear();
}

private void btnScan_Click_1(object sender, EventArgs e)
{

    List<string> search = Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*").ToList();
    progressBar1.Maximum = search.Count;
    //foreach (Directory.GetDirectories.search))

    foreach(string item in search)
    {
        try
        {
            StreamReader stream = new StreamReader(item);
            string read = stream.ReadToEnd();
            foreach(string st in viruslist)
            {
                if(Regex.IsMatch(read,st));
                {
                    viruses+=1;
                    label1.Text+= listBox1.Items.Count;
                    listBox1.Items.Add(item);
                }
                progressBar1.Increment(1);
            }
        }
        catch(Exception ex)
        {
        }
    }
}

This code is scaning all files in root folder only, but not in subfolders. How to change this code so it can scan all files in folder and subfolder too?

like image 649
mapix Avatar asked Jan 31 '26 10:01

mapix


1 Answers

Since you are using the Directory class, just use the SearchOption parameter on your call to GetFiles as so:

Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*",SearchOption.AllDirectories).ToList();

Link to MSDN

like image 116
Icarus Avatar answered Feb 02 '26 01:02

Icarus