增加C#代码中的版本号

时间:2014-09-22 07:10:11

标签: c# asp.net-mvc-4 version-control version

我在数据库中维护文档版本。假设我想更新或增加版本号。我试过这个,但它告诉我属性是只读的吗?

string _versionDB = "1.0.0.0"

var version = new Version(_versionDB);
version.MajorRevision = version.Minor + 1; //error it says read only

任何人都可以详细说明增量版本号的正确方法吗?

2 个答案:

答案 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();

答案 1 :(得分: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
}