加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在目录c#中查找具有匹配模式的文件?

发布时间:2020-12-15 18:06:42 所属栏目:百科 来源:网络整理
导读:string fileName = ""; string sourcePath = @"C:vish"; string targetPath = @"C:SR"; string sourceFile = System.IO.Path.Combine(sourcePath,fileName); string destFile = System.IO.Path.Combine(targetPath,fileName); string pattern = @"23456780
string fileName = "";

            string sourcePath = @"C:vish";
            string targetPath = @"C:SR";

            string sourceFile = System.IO.Path.Combine(sourcePath,fileName);
            string destFile = System.IO.Path.Combine(targetPath,fileName);

            string pattern = @"23456780";
            var matches = Directory.GetFiles(@"c:vish")
                .Where(path => Regex.Match(path,pattern).Success);

            foreach (string file in matches)
            {
                Console.WriteLine(file); 
                fileName = System.IO.Path.GetFileName(file);
                Console.WriteLine(fileName);
                destFile = System.IO.Path.Combine(targetPath,fileName);
                System.IO.File.Copy(file,destFile,true);

            }

我的上述程序适用于单一模式.

我正在使用上面的程序来查找具有匹配模式的目录中的文件但在我的情况下我有多个模式所以我需要将字符串模式变量中的多个模式作为数组传递但我不知道我是怎么回事可以在Regex.Match中操纵这些模式.

谁能帮我?

解决方法

你可以在正则表达式中加一个OR:
string pattern = @"(23456780|otherpatt)";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读