在 C# 中按名称比较文件
Compare files by names in c#
我有一个例程应该比较具有以下模式的文件夹中的文件:
path_to_file/some_common_base*.ext
其中 path_to_file
、common_base
和 .ext
必须匹配,但 *
部分可能会更改。
现在我遇到的问题是,我可能 path_to_file
带有正斜杠或反斜杠(C:\tmp\...
或 C:/tmp/...
)。
就性能和简单性而言,比较文件是否匹配所需模式的建议方法是什么?
感谢上面的评论,我最终使用了以下方法,比较文件名(我知道扩展名是有效的):
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...
}
}
我有一个例程应该比较具有以下模式的文件夹中的文件:
path_to_file/some_common_base*.ext
其中 path_to_file
、common_base
和 .ext
必须匹配,但 *
部分可能会更改。
现在我遇到的问题是,我可能 path_to_file
带有正斜杠或反斜杠(C:\tmp\...
或 C:/tmp/...
)。
就性能和简单性而言,比较文件是否匹配所需模式的建议方法是什么?
感谢上面的评论,我最终使用了以下方法,比较文件名(我知道扩展名是有效的):
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...
}
}