加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 使用Regex从AssemblyInfo.cs文件中检索程序集版本

发布时间:2020-12-15 08:19:58 所属栏目:百科 来源:网络整理
导读:在AssemblyInfo.cs文件中有这个字符串:[assembly:AssemblyVersion(“1.0.0.1”)],我试图逐个检索其中的数字,每个都是以下结构中的变量. static struct Version{ public static int Major,Minor,Build,Revision;} 我正在使用此模式尝试检索数字: string Ve
在AssemblyInfo.cs文件中有这个字符串:[assembly:AssemblyVersion(“1.0.0.1”)],我试图逐个检索其中的数字,每个都是以下结构中的变量.
static struct Version
{
  public static int Major,Minor,Build,Revision;
}

我正在使用此模式尝试检索数字:

string VersionPattern = @"[assembly: AssemblyVersion(""(d{1,}).(d{1,})"")]";

但是,当我使用此代码时,结果不是预期的,而是显示完整的字符串,就好像它是真正的匹配而不是组中的每个数字.

Match match = new Regex(VersionPattern).Match(this.mContents);
if (match.Success)
{
  bool success = int.TryParse(match.Groups[0].Value,Version.Major);
  ...
}

在这种情况下,this.mContents是从文件读取的整个文本和match.Groups [0] .Value应该是AssemblyVersion中的“1”

我的问题是用Regex逐个检索这些数字.

这个小工具是每次Visual Studio构建它时增加应用程序版本,我知道有很多工具可以做到这一点.

解决方法

第一组显示完全匹配.您的版本号在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,...)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读