我目前有这个小的.exe来增加我的程序的版本号。这个.exe在我的.sln构建之前在Jenkins中构建,然后更改.sln文件的global.cs。我们对程序进行了不兼容的API更改,现在希望次要值从1.1.xxx更改为1.2.xxx。为了实现我的目标,我应该改变什么。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ApplicationName.VersionUpdater
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 2) {
Console.WriteLine("Usage: ApplicationName.VersionUpdater PathToGlobal.cs RevisionNo");
return;
}
FileInfo file;
try
{
file = new FileInfo(args[0]);
}
catch (Exception ex)
{
Console.WriteLine("Invalid use: Global.cs pointer incorrect: {0}", ex.Message);
return;
}
if (!file.Exists) {
Console.WriteLine("Invalid use: Global.cs not found incorrect: {0}", file.FullName);
return;
}
int revno;
try
{
revno = int.Parse(args[1]);
}
catch (Exception ex)
{
Console.WriteLine("Invalid use: Revision number incorrect: {0}", ex.Message);
return;
}
try
{
string content = File.ReadAllText(file.FullName);
content = Regex.Replace(content, @"(?<=public const string ThisVersion = ""\d+\.\d+\.\d+\.)\d+", revno.ToString());
File.WriteAllText(file.FullName, content);
}
catch (Exception ex)
{
Console.WriteLine("Exception updating Global.cs! {0}", ex.Message);
return;
}
}
}
}
**我只需要一种方法从文件中读取正则表达式代码,然后将其调整为我想要的。如何从此文件中读取正则表达式代码以及如何手动更改? **
答案 0 :(得分:1)
代码中的Regex.Replace(...)
行表示要查找以public const string ThisVersion = "
开头的字符串,后跟三个数字,后跟一个点。例如public const string ThisVersion = "12.34.56.
。然后该行查找另一个数字,它将替换为作为命令行参数传入的数字。
所以用(例如)ApplicationName.VersionUpdater TheGlobal.cs 42
调用该实用程序会导致此字符串
public const string ThisVersion = "12.34.56.78
替换为
public const string ThisVersion = "12.34.56.42
请注意,正则表达式不会查看或修改第四个数字后的任何字符。
此实用程序支持的版本号与问题的1.1.xxx
和1.2.xxx
不匹配。它们与1.1.1.xxx
和1.1.2.xxx
或1.2.1.xxx
。
您的解决方案有两个步骤。
手动编辑相关的...Global.cs
文件,将1
更改为2
。
如果需要将最后一个数字重新启动到1
(或其他一些值),那么找到生成实用程序的RevisionNo
参数的位置和方式,并将其更改为从{开始{1}}。