:我的應用程序演練遞歸到驅動器/文件夾的用戶指定(通過的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