简体   繁体   中英

How can I get the assembly version as a integer in C#?

I can get it as a string:

Assembly.GetExecutingAssembly().GetName().Version()  

How can I convert this number to an int?


Example input:

8.0.0.1

Expected output:

8001

If you really have to do this conversion, you should at least reserve an adequate amount of digits for every version part.

Two digits for Major, Minor and Revision and four for Build:

var version = Assembly.GetExecutingAssembly().GetName().Version();

Debug.Assert(version.Major >= 0 && version.Major < 100)
Debug.Assert(version.Minor >= 0 && version.Minor < 100)
Debug.Assert(version.Build >= 0 && version.Build < 10000)
Debug.Assert(version.Revision >= 0 && version.Revision < 100)

long longVersion = version.Major * 100000000L + 
                   version.Minor * 1000000L + 
                   version.Build * 100L + 
                   version.Revision;

int intVersion  = (int) longVersion;

Debug.Assert((long)intVersion == longVersion)

Note that this method will still fail for some exotic versions!

Don't even think about version 21.47.4836.48 :)

EDIT: Added assertions.

They are numbers, so you could just convert them to strings and stack them after each other:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
string compacted = string.Format("{0}{1}{2}{3}", v.Major, v.Minor, v.Build, v.Revision);
int compactInt = int.Parse(compacted);

This of course only works well as long as each component is less than ten. After that you can't determine what the actual version is. The string "12345" can be 1.23.4.5 or 12.3.4.5 or 1.2.34.5 or...

If you know that they are all below ten, you could do it numerically:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
int compactInt = v.Major * 1000 + v.Minor * 100 + v.Build * 10 + v.Revision;

If you don't know that, it gets a bit more complicated, but you can still do it without creating a string and parse it. It would be a bit faster, but it's 10 - 20 lines of code.

Another option would be to treat it like a 4 byte integer. You will be limited to the values of 0-255 for each of the version parts (major, minor, build, revision), but you can pack the version into an integer value.

This prevents the problem of 8.10.0.0 being greater than 9.0.0.0 (81,000 > 9,000).

    /// <summary>
    ///  
    /// Converts a version string in the format of 
    /// [major].[minor].[build].[revision] into an integer.
    ///  
    /// </summary>
    /// <remarks>
    /// Each part of the build number must be within the range of 0 to 255.
    /// </remarks>
    /// <param name="version"> The version to convert to an integer. </param>
    /// <returns>
    /// The integer representation of the <paramref name="version"/>.
    /// </returns>
    public static int VersionToInt( string version ) {
        // Parse version number
        var versionParts = version.Split( '.' );
        if ( versionParts.Length != 4 ) {
            throw new ArgumentException( "Invalid version number; invalid number of version parts, expected 4 (major.minor.build.revision)",
                                         "version" );
        }

        // Convert parts to bytes
        byte major, minor, build, revision;
        if ( !byte.TryParse( versionParts[0], out major ) ) {
            throw new ArgumentException( "Invalid version number; invalid major number", 
                                         "version" );
        }
        if ( !byte.TryParse( versionParts[1], out minor ) ) {
            throw new ArgumentException( "Invalid version number; invalid minor number",
                                         "version" );
        }
        if ( !byte.TryParse( versionParts[2], out build ) ) {
            throw new ArgumentException( "Invalid version number; invalid build number", 
                                         "version" );
        }
        if ( !byte.TryParse( versionParts[3], out revision ) ) {
            throw new ArgumentException( "Invalid version number; invalid revision number", 
                                         "version" );
        }

        // Combine bytes into an integer
        var versionInteger = 0;
        versionInteger |= major << 24;
        versionInteger |= minor << 16;
        versionInteger |= build << 8;
        versionInteger |= revision;

        return versionInteger;
    }

你可以 string.Split('.'), iterate 和 int.parse()

The following 'works':

string version = "8.0.0.1";
int versionNumber = int.Parse(version.Replace(".", ""));

However, there would be no difference between version 8.0.11.0 and 8.0.1.10!

// assuming v is Version
var versionNumber = long.Parse(v.Major.ToString("x2") + v.Minor.ToString("x2") + v.Build.ToString("x2"), Globalization.NumberStyles.HexNumber);

I would just do this:

Integer.Parse(Assembly.GetExecutingAssembly().GetName().Version().ToString().Replace(".", ""))

I stand Corrected.

您还可能遇到这样的情况:程序集的版本为 0.0.1.1,然后版本看起来像:11。绝对不需要。

Try this:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
long s = long.Parse(v.Major.ToString("0000") + v.MajorRevision.ToString("0000") + v.Minor.ToString("0000") + v.MinorRevision.ToString("0000"));

What is the problem you're trying to solve? Are you checking to see if the running version of some library is greater than or equal to some known bug-free version? If so, the right thing to do is not to treat a .-delimited string of numbers as a single number, but to compare them individually to your target.

Rather than that approach, and assuming that each of your version string elements is less than 255, you could just take each element as a byte in a 32 bit unsigned integer. This would not be very human readable, but would ensure that if V1 < V2 in version comparison then f(V1) < f(V2) for the integer conversion f.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM