查找文件在目录c#匹配模式? [英] Find files with matching patterns in a directory c#?

查看:326
本文介绍了查找文件在目录c#匹配模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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);

            }

我上面的程序可以很好地与一个单一的模式。

My above program works well with a single pattern.

我用上面的程序来查找目录中的文件与匹配的模式,但在我的情况我有多个模式,所以我需要传递多个图案字符串模式变量作为数组,但我没有任何想法如何,我可以操纵这些图案Regex.Match。

I'm using above program to find the files in a directory with a matching pattern but in my case I've multiple patterns so i need to pass multiple pattern in string pattern variable as an array but I don't have any idea how i can manipulate those pattern in Regex.Match.

谁能帮我?

推荐答案

您可以把一个正则表达式

string pattern = @"(23456780|otherpatt)";

这篇关于查找文件在目录c#匹配模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆