:我的应用程序演练递归到驱动器/文件夹的用户指定(通过的FolderBrowserDialog),去正则表达式模式没有显示我有以下代码匹配
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);
}
}
什么我tryign做到的是通过我的if语句。如果文件包含我的正则表达式模式中定义的任何字符,它将输出到我的datagridview。如果不是,则不要在datagridview上显示它。
由于某种原因,我的代码似乎会拾取文件夹中的所有文件 - 不仅仅是具有RegEx模式中的字符的文件。我已经看了很长一段时间了,我不确定这是为什么会发生。任何人有任何想法,也许我不捕捉?
你有一些示例文件名的名称? – Iain 2010-06-29 15:55:33