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

I can get it as a string:

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

How can I convert this number to int?


Input Example:

8.0.0.1

Expected Result:

8001

+3
source share
10 answers

If you really need to do this conversion, you should at least reserve a sufficient number of digits for each part of the version.

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

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)

Please note that this method will still not work for some exotic versions!

Do not even think about version 21.47.4836.48 :)

EDIT: Added statements.

+12
source

, :

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);

, , , . , . "12345" 1.23.4.5 12.3.4.5 1.2.34.5 ...

, , :

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

, , , . , 10-20 .

+3

string.Split('.'), int.parse()

+1

"":

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

8.0.11.0 8.0.1.10 .

+1

4 . 0-255 (, , , ), .

8.10.0.0, 9.0.0.0 (81 000 > 9000).

    /// <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;
    }
+1

:

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

.

0

, 0.0.1.1, : 11. .

0

:

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"));
0

, ? , - - ? , - . , .

0

, 255, 32- . , , V1 < V2 , f (V1) f (V2) f.

0

Source: https://habr.com/ru/post/1710262/


All Articles