如何在c#.net中自动增加程序集的版本 [英] How to automatically increment version of assemblies in c#.net

查看:373
本文介绍了如何在c#.net中自动增加程序集的版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在C#.Net中自动安装版本程序集。



我的要求如下:

1.在我的解决方案中我有多个DLL和EXE,它们都是构建和部署的。

2.我想用相同的版本号对它们进行版本化,当我在Release模式下构建我的解决方案时,它应该自动增加,无论是手动构建还是自动构建。





我需要实现的是它应该自动增加XX * .X,其中*是内部版本号。 />


如果有人以前遇到过这种情况,请告诉我。可以帮助。



谢谢



我尝试过的事情:



我尝试了以下但没有帮助:



1.修改AssemblyInfo.cs文件,在版本号中加上'*'代替零,如1.0。*

2 。本文中的后续步骤,组装版本的正确方法 [ ^ ]

解决方案

我在<$中有以下内容c $ c> BuildVersion.cs 这是项目解决方案文件夹中的链接文件

 使用 System.Reflection; 
// 内部版本号= 368
// 构建版本= 3.3.14

[程序集:AssemblyVersion( 3.0.0.0)]
[ assembly :AssemblyFileVersion( 3.3.14.368)]



在其中一个项目预建活动中使用以下行:


< blockquote>(SolutionDir)tools\buildversion.exe


(SolutionDir)buildversion.cs



内部版本号将在每次构建时递增,您可以在需要时手动更改的版本号, AssemblyVersion AssemblyFileVersion 是不同的,所以如果您使用GAC它将覆盖非破坏性更改但是如果你在资源管理器中查看它,你仍然可以获得文件版本。

使用以下代码完成增量,编译为exe:

 使用系统; 
使用 System.IO;
使用 System.Windows.Forms;
使用 System.Text.RegularExpressions;

命名空间 pp
{
public class pp
{
public static void Main( string [] args)
{
if (args.Length< 1) return ;

string filename = args [ 0 ];
string buildversion = ;
int buildnum = 0 ;


StreamReader sr = new StreamReader(filename);

string s = sr.ReadToEnd();
sr.Close();
string buildnumregex = @ \ s * // * \s * build\s number\s * = \s *(小于?NUM> \w *);
string buildverregex = @ \ s * // * \s * build\s version\s * = \s *(小于?VERS> [\w。] *);
Regex mr = new 正则表达式(
buildnumregex,
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
);

匹配m = mr.Match(s);
string ss = m.Groups [ NUM]值。
if (ss!=
{
buildnum = int .Parse(ss);
buildnum ++;
s = mr.Replace(s, \r\ n // build number = + buildnum.ToString());
}

mr = new 正则表达式(
buildverregex,
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
);

m = mr.Match(s);
ss = m.Groups [ vers]。
if (ss!= )buildversion = ss;

string currbuild = buildversion + + buildnum.ToString();

mr = new 正则表达式(
@ AssemblyFileVersion\s * \((?< ver> [\\\。*])
RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
);

m = mr.Match(s);
s = mr.Replace(s, AssemblyFileVersion(\ + currbuild + \ );

// MessageBox.Show(s);

StreamWriter sw = new StreamWriter(args [ 0 ]);
sw.Write(s);
sw.Close();
}
}
}


How to Automatically Version Assemblies in C#.Net.

I have a requirement as follows:
1. In my solution I have multiple DLLs and EXE which are built and deployed.
2. I would like to version them all with same version numbers and it should increment automatically when I build my solution in Release mode, either manual build or auto build.


what I need to achieve is it should automatically increment X.X.*.X where * is the build number.

Please let me know if anyone have encountered this situation before and can help.

Thank you

What I have tried:

I have tried below things but no help:

1. Modified AssemblyInfo.cs file by providing '*' in place of zero's in version number like "1.0.*"
2. Followed steps in this article, The Right Way to Version Your Assemblies[^]

解决方案

I have the following in BuildVersion.cs which is a linked file in the solution folder of the projects :

using System.Reflection;
// build number = 368
// build version = 3.3.14

[assembly: AssemblyVersion("3.0.0.0")]
[assembly: AssemblyFileVersion("3.3.14.368")]


With the following line in one of the projects pre build event :

"


(SolutionDir)tools\buildversion.exe" "


(SolutionDir)buildversion.cs"


The build number will be incremented on each build, the version numbers you can change by hand when needed, AssemblyVersion and AssemblyFileVersion are different so if you use the GAC it will overwrite for non breaking changes but you still get the file version if you view it in Explorer.
And the increment is done with the following code which is compiled to an exe :

using System;
using System.IO;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace pp
{
	public class pp
	{
		public static void Main(string[] args)
		{
			if(args.Length<1) return;
			
			string filename = args[0];
			string buildversion = "";
			int buildnum = 0;
			
			
			StreamReader sr = new StreamReader(filename);
			
			string s = sr.ReadToEnd();
			sr.Close();
			string buildnumregex = @"\s*//\s*build\s*number\s*=\s*(?<num>\w*)";
			string buildverregex = @"\s*//\s*build\s*version\s*=\s*(?<vers>[\w.]*)";
			Regex mr = new Regex(
				buildnumregex,
				RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
				);
			
			Match   m = mr.Match(s);
			string ss= m.Groups["num"].Value;
			if(ss !="") 
			{
				buildnum = int.Parse(ss);
				buildnum++;
				s=mr.Replace(s,"\r\n// build number = "+buildnum.ToString());
			}
			
			mr = new Regex(
				buildverregex,
				RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
				);
			
			m = mr.Match(s);
			ss =m.Groups["vers"].Value;
			if(ss !="") buildversion = ss;
			
			string currbuild = buildversion+"."+buildnum.ToString();
			
			mr = new Regex(
				@"AssemblyFileVersion\s*\(""(?<ver>[\w.]*"")",
				RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace
				);
			
			m=mr.Match(s);
			s=mr.Replace(s,"AssemblyFileVersion(\""+ currbuild + "\"");

			//MessageBox.Show(s);
			
			StreamWriter sw = new StreamWriter(args[0]);
			sw.Write(s);
			sw.Close();
		}
	}
}


这篇关于如何在c#.net中自动增加程序集的版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆