private void DirSearch(string root, string filesExtension,string textToSearch, BackgroundWorker worker, DoWorkEventArgs e)
{
string[] filePaths = null;
int numberoffiles = 0;
int numberofdirs = 0;
try
{
filePaths = Directory.GetFiles(root, filesExtension, SearchOption.AllDirectories);
}
catch(Exception err)
{
string ad = err.ToString();
}
if (filePaths != null && filePaths.Length > 0)
{
for (int i = 0; i < filePaths.Length; i++)
{
_busy.WaitOne();
if (worker.CancellationPending == true)
{
e.Cancel = true;
return;
}
List<MyProgress> prog = new List<MyProgress>();
int var = File.ReadAllText(filePaths[i]).Contains(textToSearch) ? 1 : 0;
if (var == 1)
{
string filename = filePaths[i];//filePaths[i].Split('\\').Last();
numberoffiles++;
prog.Add(new MyProgress { Report1 = filename, Report2 = numberoffiles.ToString() });
backgroundWorker1.ReportProgress(0, prog);
Thread.Sleep(100);
}
numberofdirs++;
label1.Invoke((MethodInvoker)delegate
{
label1.Text = numberofdirs.ToString();
label1.Visible = true;
});
Thread.Sleep(100);
}
}
}
I'm using try and catch the problem is that once one of the directories is access denied the program stop. And i want it to continue to the next directory.
If in filePaths for example i have 450 files in 10 directories and in the second directory it's access denied then continue to the next directory and so on.