我可以将其作为字符串获取:
Assembly.GetExecutingAssembly().GetName().Version()
如何将此数字转换为整数?
示例输入:
8.0.0.1
预期输出:
8001
我可以将其作为字符串获取:
Assembly.GetExecutingAssembly().GetName().Version()
如何将此数字转换为整数?
8.0.0.1
8001
如果你真的必须进行这种转换,那么你应该至少为每个版本部分保留足够的数字位数。
主版本、次版本和修订版本需要两位数字,构建版本需要四位数字:
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)
需要注意,这种方法仍然无法处理某些奇特的版本!
别想着用版本号21.47.4836.48 :)
编辑: 添加了断言。
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);
Version v = Assembly.GetExecutingAssembly().GetName().Version;
int compactInt = v.Major * 1000 + v.Minor * 100 + v.Build * 10 + v.Revision;
如果你不知道这个,那么就会更加复杂一些,但是你仍然可以在不创建字符串并解析它的情况下完成。这样会快一点,但需要10到20行代码。
/// <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 version = "8.0.0.1";
int versionNumber = int.Parse(version.Replace(".", ""));
然而,版本8.0.11.0和8.0.1.10之间没有任何区别!
Integer.Parse(Assembly.GetExecutingAssembly().GetName().Version().ToString().Replace(".", ""))
我改正了。
试试这个:
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.1.1,然后版本号看起来像是11。显然不是期望的结果。