Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在我的版本更新程序中更改次要值_C#_Regex - Fatal编程技术网

C# 在我的版本更新程序中更改次要值

C# 在我的版本更新程序中更改次要值,c#,regex,C#,Regex,我现在有一个小的.exe来增加我程序的版本号。此.exe在生成my.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

我现在有一个小的.exe来增加我程序的版本号。此.exe在生成my.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;
            }

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Text.RegularExpressions;
命名空间ApplicationName.VersionUpdate
{
班级计划
{
静态void Main(字符串[]参数)
{
如果(参数长度!=2){
Console.WriteLine(“用法:ApplicationName.versionUpdate-PathToGlobal.cs修订号”);
返回;
}
文件信息文件;
尝试
{
file=newfileinfo(args[0]);
}
捕获(例外情况除外)
{
WriteLine(“无效用法:Global.cs指针不正确:{0}”,例如Message);
返回;
}
如果(!file.Exists){
WriteLine(“无效用法:找不到Global.cs错误:{0}”,file.FullName);
返回;
}
国际修订号;
尝试
{
revno=int.Parse(args[1]);
}
捕获(例外情况除外)
{
WriteLine(“无效使用:修订号不正确:{0}”,例如Message);
返回;
}
尝试
{
字符串内容=File.ReadAllText(File.FullName);
content=Regex.Replace(content,@”(?)代码中的
Regex.Replace(…)
行表示查找以
public const string ThisVersion=“
后跟三个数字,每个数字后跟一个点开头的字符串。例如
public const string ThisVersion=“12.34.56.
。然后,该行查找另一个数字,并将其替换为作为命令行参数传入的数字

因此,使用(比如)ApplicationName.versionUplater TheGlobal.cs42
调用该实用程序将导致以下字符串

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
    开始