在 C# 代码中增加版本号
Posted
技术标签:
【中文标题】在 C# 代码中增加版本号【英文标题】:Increment Version Number in C# Code 【发布时间】:2014-11-16 02:02:30 【问题描述】:我正在数据库中维护文档版本。假设我想更新或增加版本号。我试过了,但它显示属性是只读的?
string _versionDB = "1.0.0.0"
var version = new Version(_versionDB);
version.MajorRevision = version.Minor + 1; //error it says read only
谁能详细说明增加版本号的正确方法?
【问题讨论】:
因为System.Version 的属性是只读的。为什么你不能只实现你自己的功能来分配价值 请你在这里放一些示例代码我怎样才能实现我自己的功能? 这取决于您想要的格式。最简单的是将值存储为 int,然后递增它。如果您真的想要上面显示的内容,那么您需要拆分字符串,将值转换为数字,增加一个或多个,然后将它们连接回字符串 【参考方案1】:您可以创建一个扩展方法来为您执行添加。仍然需要自定义代码;但是,如果您在很多地方都这样做,那么使用这种辅助方法可能会很有用。以下是一些可能会有所帮助的此类功能。
public static class VersionExtension
public static Version IncrementRevision(this Version version)
return AddVersion(version, 0, 0, 0, 1);
public static Version IncrementBuild(this Version version)
return IncrementBuild(version, true);
public static Version IncrementBuild(this Version version, bool resetLowerNumbers)
return AddVersion(version, 0, 0, 1, resetLowerNumbers ? -version.Revision : 0);
public static Version IncrementMinor(this Version version)
return IncrementMinor(version, true);
public static Version IncrementMinor(this Version version, bool resetLowerNumbers)
return AddVersion(version, 0, 1, resetLowerNumbers ? -version.Build : 0, resetLowerNumbers ? -version.Revision : 0);
public static Version IncrementMajor(this Version version)
return IncrementMajor(version, true);
public static Version IncrementMajor(this Version version, bool resetLowerNumbers)
return AddVersion(version, 1, resetLowerNumbers ? -version.Minor : 0, resetLowerNumbers ? -version.Build : 0, resetLowerNumbers ? -version.Revision : 0);
public static Version AddVersion(this Version version, string addVersion)
return AddVersion(version, new Version(addVersion));
public static Version AddVersion(this Version version, Version addVersion)
return AddVersion(version, addVersion.Major, addVersion.Minor, addVersion.Build, addVersion.Revision);
public static Version AddVersion(this Version version, int major, int minor, int build, int revision)
return SetVersion(version, version.Major + major, version.Minor + minor, version.Build + build, version.Revision + revision);
public static Version SetVersion(this Version version, int major, int minor, int build, int revision)
return new Version(major, minor, build, revision);
/*
//one day we may even be able to do something like this...
//https://github.com/dotnet/csharplang/issues/192
public static Version operator +(Version version, int revision)
return AddVersion(version, 0, 0, 0, revision);
public static Version operator ++(Version version)
return IncrementVersion(version);
*/
示例用法
void Main()
Version myVersion = new Version ("1.2.3.4");
myVersion = myVersion.IncrementRevision();
Console.WriteLine(myVersion); //1.2.3.5
myVersion = myVersion.IncrementMinor();
Console.WriteLine(myVersion); //1.3.0.0
【讨论】:
【参考方案2】:我知道这有点老了,但对于任何寻找如何做到这一点的人来说,这可能会有所帮助:
string _versionDB = "1.0.0.0"
var version = new Version(_versionDB);
// Increment Major version
var newMajorVersion = new Version(version.Major+1,version.Minor,version.Build,version.Revision);
// Increment Minor version
var newMinorVersion = new Version(version.Major,version.Minor+1,version.Build,version.Revision);
// Increment Build version
var newBuildVersion = new Version(version.Major,version.Minor,version.Build+1,version.Revision);
// Increment Revision
var newRevisionVersion = new Version(version.Major,version.Minor,version.Build,version.Revision+1);
返回字符串以保存回数据库(基于您的变量名的假设):
_versionDB = newMajorVersion.ToString();
【讨论】:
以上是关于在 C# 代码中增加版本号的主要内容,如果未能解决你的问题,请参考以下文章