I have the following code:
public void DriveRecursion(string retPath)
{
string pattern = @"[~#&!%\+\{\}]+";
Regex regEx = new Regex(pattern);
string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);
List<string> filePath = new List<string>();
List<string> filePaths = new List<string>();
dataGridView1.Rows.Clear();
try
{
foreach (string fileNames in fileDrive)
{
SanitizeFileNames sw = new SanitizeFileNames();
if (regEx.IsMatch(fileNames))
{
string fileNameOnly = Path.GetFileName(fileNames);
string pathOnly = Path.GetDirectoryName(fileNames);
DataGridViewRow dgr = new DataGridViewRow();
filePath.Add(fileNames);
dgr.CreateCells(dataGridView1);
dgr.Cells[0].Value = pathOnly;
dgr.Cells[1].Value = fileNameOnly;
dataGridView1.Rows.Add(dgr);
//filePath.Add(fileNames);
filePaths.Add(fileNames);
paths.Add(fileNames);
//sw.FileCleanup(filePaths);
}
else
{
continue;
//DataGridViewRow dgr2 = new DataGridViewRow();
//dgr2.Cells[0].Value = "No Files To Clean Up";
//dgr2.Cells[1].Value = "";
}
}
}
catch (Exception e)
{
StreamWriter sw = new StreamWriter(retPath + "ErrorLog.txt");
sw.Write(e);
}
}
What i'm tryign to accomplish is that my application drills recursively into a drive/folder that the user specifies (through a FolderBrowserDialog) and goes through my if statement. IF the file contains any of the chars defined in my regex pattern, it gets output to my datagridview. If it does NOT, then do not show it on the datagridview.
For some reason right now, my code seems to pick up all files in a folder--not just specifically the ones with the chars in my RegEx pattern. I've been looking at this for quite some time and i'm not sure exactly why this is happening. Anybody have any ideas that perhaps I'm not catching?