0

我有一个对应于变量“路径”的文件夹位置。在这个文件夹中,我有很多文件,但只有一个名为“common.build.9897ytyt4541”。我想要做的是读取该文件的内容,因此使用以下内容,它正在工作:

string text = File.ReadAllText(Path.Combine(path, "common.build.9897ytyt4541.js"));

问题是,“构建”和“js”之间的部分,在每次源代码编译时都会发生变化,我得到一个新的哈希,所以我想替换以前的代码,在每次构建时都有一些工作,无论如何哈希是,我想用正则表达式,但这不起作用:

string text = File.ReadAllText(Path.Combine(path, @"common.build.*.js"));

在此先感谢您的帮助

4

2 回答 2

2

如果你知道你只会找到一个文件,你可以写这样的东西(加上错误处理):

using System.Linq;
...
var filePath = Directory.GetFiles(path, "common.build.*.js").FirstOrDefault();
string text = File.ReadAllText(filePath);
于 2019-02-06T16:25:28.617 回答
1

不,您需要在 using 中使用确切的文件名File.ReadAllText。相反,您需要搜索文件,为此您可以使用Directory.GetFiles,例如:

var matches = Directory.GetFiles(path, "common.build.*.js");

if(matches.Count() == 0)
{
    //File not found
}
else if(matches.Count() > 1)
{
    //Multiple matches found
}
else
{
    string text = File.ReadAllText(matches[0]);
}
于 2019-02-06T16:25:42.520 回答