在c#中按名称比较文件

时间:2017-03-13 12:27:58

标签: c# pattern-matching filenames

我有一个例程,应该比较文件夹中具有以下模式的文件:

java.lang.NullPointerException: null

其中path_to_file/some_common_base*.ext path_to_filecommon_base必须匹配,但.ext部分可能会发生变化。

现在我遇到的问题是,我可能会*使用向前或向后斜杠(path_to_fileC:\\tmp\\...)。

在性能和简单性方面,比较文件是否符合所需模式的建议方法是什么?

1 个答案:

答案 0 :(得分:0)

感谢上面的评论,我最终使用了以下方法,比较文件名(我知道扩展名):

string testingFile = "....";
DirectoryInfo dir = new DirectoryInfo("path_to_file");
foreach (FileInfo f in dir.GetFiles("*.ext")) {
    if (f.Name.ToUpperInvariant().StartsWith("some_common_base")) {
       // f is mathing requirement...
    }
}