[补充]使用正则表达式更改编译版本号

本文介绍了一种使用正则表达式进行版本号替换的方法,并实现了一个自动递增修订号的功能。通过读取文件内容并应用特定模式的正则表达式,可以将指定的版本号替换成新的版本号,并在配置允许的情况下自动增加修订号。
 1 string fileContents = readFile(fName);
 2                 rgx = new Regex(pattern);
 3 
 4                 {
 5                     switch (verArg.Length)
 6                     {
 7                         case 4:  //<major version>.<minor version>.<build number>.<revision> 
 8                             replacement = String.Format("${{4}}{0}${{5}}{1}${{6}}{2}${{7}}{3}", verArg);
 9                             break;
10                         case 3:
11                             replacement = String.Format("${{3}}{0}${{4}}{1}${{5}}{2}", verArg);
12                             break;
13                         case 2:
14                             replacement = String.Format("${{2}}{0}${{3}}{1}", verArg);
15                             break;
16                         case 1:
17                             replacement = String.Format("${{1}}{0}", verArg);
18                             break;
19                     }
20                 }
21 
22  // Replace the build component of the AssemblyVersion attribute 
23                 // with the specified build number.
24                 fileContents = rgx.Replace(fileContents, replacement);
25 
26                 // reversion++
27                 if (ConfigurationManager.AppSettings["autoreversion"== "true")
28                 {
29                     // 读取匹配模式的行
30                     Match matchLine = rgx.Match(fileContents);
31                     string strLine = fileContents.Substring(matchLine.Groups[0].Index);
32                     strLine = strLine.Substring(0, strLine.IndexOf("\r\n"));
33               
34                     // 匹配四段版本号
35                     Regex regex = new Regex(@"(?<Major>[0-9]+)\s*[\.,]\s*(?<Minor>[0-9]+)\s*[\.,]\s*(?<Build>[0-9]+)\s*[\.,]\s*(?<Revision>[0-9]+)");
36                     Match m = regex.Match(strLine);
37 
38                     // 获取Revision
39                     string strRevision = m.Groups["Revision"].Value;
40                     string strLineNew = strLine.Substring(0, m.Groups["Revision"].Index);
41                     strLineNew += (int.Parse(strRevision) + 1) ;
42 
43                     // 更新编译序号
44                     fileContents = fileContents.Replace(strLine, strLineNew);
45 
46                 }
47                 // Open AssemblyVersionInfo file for Write access
48                 // Note: FileMode.Truncate effectively deletes the contents
49                 // of the file.
50                 WriteFile(fName, fileContents, true);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值