[英]Retrieving Assembly version from AssemblyInfo.cs file with Regex
Inside the AssemblyInfo.cs file there's this string: [assembly: AssemblyVersion("1.0.0.1")]
and I am trying to retreive one by one the numbers in it, each one is a variable in the following structure. 在AssemblyInfo.cs文件中有这个字符串: [assembly: AssemblyVersion("1.0.0.1")]
,我试图逐个检索其中的数字,每个都是以下结构中的变量。
static struct Version
{
public static int Major, Minor, Build, Revision;
}
I am using this pattern to try to retrieve the numbers: 我正在使用此模式尝试检索数字:
string VersionPattern = @"\[assembly\: AssemblyVersion\(""(\d{1,})\.(\d{1,})\.(\d{1,})\.(\d{1,})""\)\]";
However, when I use this code the result is not as expected, instead it shows the full string as if it was the real match and not each number in a Group. 但是,当我使用此代码时,结果不是预期的,而是显示完整的字符串,就好像它是真正的匹配而不是组中的每个数字。
Match match = new Regex(VersionPattern).Match(this.mContents);
if (match.Success)
{
bool success = int.TryParse(match.Groups[0].Value,Version.Major);
...
}
In this case this.mContents
is the whole text read from the file and match.Groups[0].Value
should be "1" from the AssemblyVersion 在这种情况下, this.mContents
是从文件读取的整个文本和match.Groups[0].Value
应该是AssemblyVersion中的“1”
My question is to retrieve these numbers one by one with Regex. 我的问题是用Regex逐个检索这些数字。
This small tool is to increment the application version everytime Visual Studio builds it and I know there are many tools out there to do this. 这个小工具是每次Visual Studio构建它时增加应用程序版本,我知道有很多工具可以做到这一点。
The first group is showing the full match. 第一组显示完全匹配。 Your version numbers are in groups 1-4: 您的版本号在1-4组中:
int.TryParse(match.Groups[1].Value, ...)
int.TryParse(match.Groups[2].Value, ...)
int.TryParse(match.Groups[3].Value, ...)
int.TryParse(match.Groups[4].Value, ...)
The System.Version class will do this for you. System.Version类将为您执行此操作。 Just pass in the version string to the constructor, like so: 只需将版本字符串传递给构造函数,如下所示:
System.Version(this.mContents);
Also, a System.Version can be obtained by the following function calls: 此外,可以通过以下函数调用获取System.Version:
Assembly.GetExecutingAssembly().GetName().Version;
The build and revision numbers can also automatically be set by specifying a '*' as shown below: 也可以通过指定'*'自动设置构建和修订号,如下所示:
[assembly: AssemblyVersion("1.0.*")]
I hope this helps. 我希望这有帮助。
I stumbled on the same problem but I think it will be much easier with a slightly changed pattern. 我偶然发现了同样的问题,但我认为稍微改变模式会更容易。
private const string AssemblyVersionStart = @"\[assembly\: AssemblyVersion\(""(\d+\.\d+\.\d+\.\d+)""\)\]";
You get the version by parsing the Groups[1] which will contain something like "1.0.237.2927". 您可以通过解析包含类似“1.0.237.2927”之类的Groups [1]获得版本。
try{
var match= Regex.Match(text, AssemblyVersionStart);
var version = System.Version.Parse(match.Groups[1].Value);
...
}catch(...
Is there a reason why you must use a regexp instead of: 是否有必要使用正则表达式而不是:
string[] component = this.mContents.Split('.');
bool success = int.TryParse(component[0], out Version.Major);
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.