路径比较的正则表达式如何写??

gmlan_cn 2009-04-09 04:56:10
想删除指定路径下的所有日志文件,但是要排除其中指定的某个(多个)文件夹,代码如下:
try
{
DirectoryInfo d = new DirectoryInfo( @"\\server01\c$\windows");

DirectoryInfo[] dirs = d.GetDirectories();
foreach (DirectoryInfo item in dirs)
{
string ExcludeRegEx = @"\\server01\c$\windows\System32";
Regex reg = new Regex(ExcludeRegEx, RegexOptions.IgnoreCase);
Match match = reg.Match(item.FullName);
if (match.Success)
{
Console.WriteLine("match success");
}
else
{
Console.WriteLine("match not success");
}
}
}
catch (Exception ex)
{
throw;
}


程序第7行抛出异常,
Message = "parsing \"\\\\server01\\c$\\windows\\System32\" - Unrecognized control character."

请问ExcludeRegEx 该如何写??
...全文
67 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gmlan_cn 2009-04-09
  • 打赏
  • 举报
回复
不错,好使。
LemIST 2009-04-09
  • 打赏
  • 举报
回复
其实这里也用不到正则,只要判断下路径是不是以\\server01\c$\windows\System32开始就好了,简单的字符串匹配。
LemIST 2009-04-09
  • 打赏
  • 举报
回复
这里需要两次转义,一次是字符串的,一次是正则表达式的。
$在正则表达式里代表匹配字符串的结束,所以也许要转义。
LemIST 2009-04-09
  • 打赏
  • 举报
回复
string ExcludeRegEx = "\\\\\\\\server01\\\\c\\$\\\\windows\\\\System32";

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧