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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With