Compact Framework 取执行文件版本号

简介:

取Managed Assembly版本号

这个方法不仅仅可以取执行文件,也可以取DLL的版本号,支持所有托管的Assembly。

public string GetAppVersion(string appName)
{
try
{
Assembly assembly = Assembly.LoadFrom(appName + ".exe");
return assembly.GetName().Version.ToString();
}
catch (Exception e)
{
Logger.Instance.LogException(e, "Load Assembly fail");
}
return "Unknown";
}

取Native生成文件版本号

如果文件是使用Native code生成出来的,就没有那么简单了。

public string GetNativeAppVersion(string appName)
{
try
{
FileVersionInfo versionInfo = FileVersionInfo.GetVersionInfo(appName+ ".exe");
StringBuilder versionNumber = new StringBuilder();
versionNumber.Append(versionInfo.ProductMajorPart);
versionNumber.Append(".");
versionNumber.Append(versionInfo.ProductMinorPart);
versionNumber.Append(".");
versionNumber.Append(versionInfo.ProductBuildPart);
versionNumber.Append(".");
versionNumber.Append(versionInfo.ProductPrivatePart);

return versionNumber.ToString();
}
catch (FileNotFoundException e)
{
Logger.Instance.LogException(e, "Fail to find the File");
}
catch (Exception e)
{
Logger.Instance.LogException(e, "Fail to get native application version");
}
return "Unknown";
}
这里使用了  OpenNETCF Smart Device Framework v 1.4  的FileVersionInfo

类,取出Native文件的版本信息。下面代码修改自OpenNETCF Smart Device Framework v 1.4 的FileVersionInfo类。

//==========================================================================================
//
// OpenNETCF.Diagnostics.FileVersionInfo
// Copyright (c) 2003-2004, OpenNETCF.org
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the OpenNETCF.org Shared Source License.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the OpenNETCF.org Shared Source License
// for more details.
//
// You should have received a copy of the OpenNETCF.org Shared Source License
// along with this library; if not, email licensing@opennetcf.org to request a copy.
//
// If you wish to contact the OpenNETCF Advisory Board to discuss licensing, please
// email licensing@opennetcf.org.
//
// For general enquiries, email enquiries@opennetcf.org or visit our website at:
// http://www.opennetcf.org
//
//==========================================================================================
using using using namespace /// <summary>
///
Provides version information for a physical file in storage memory.
/// </summary>
public sealed class FileVersionInfo
//the name of the file (sans path)
private string //the language independant version info
private byteprivate const int private const int private const int #region private string //get the filename sans path
Pathint int //get size of version info
ref if//allocate buffer
//IntPtr buffer = LocalAlloc(LPTR, (uint)0xffff);
IntPtr uint//IntPtr buffer = MarshalEx.AllocHGlobal(len);
//get version information
ifIntPtr IntPtrint //get language independant version info
//this is a pointer within the main buffer so don't free it
if"\\"ref ref //allocate managed memory
new byte//copy to managed memory
Marshalelse
throw new Exception"Error retrieving language independant version"else
throw new Exception"Error retrieving FileVersionInformation"//free native buffer
//MarshalEx.FreeHGlobal(buffer);
if IntPtrIntPtr#endregion

#region
/// <summary>
///
Returns a <see cref="FileVersionInfo"/> representing the version information associated with the specified file.
/// </summary>
/// <param name="fileName">
The fully qualified path and name of the file to retrieve the version information for.</param>
/// <returns>
A <see cref="FileVersionInfo"/> containing information about the file.
/// If the file information was not found, the <see cref="FileVersionInfo"/> contains only the name of the file requested.</returns>
/// <exception cref="System.IO.FileNotFoundException">
The file specified cannot be found.</exception>
public static FileVersionInfo string //check if file exists first
ifFilereturn new FileVersionInfoelse
throw new FileNotFoundException"The specified file was not found"#endregion

#region
/// <summary>
///
Gets the name of the file that this instance of <see cref="FileVersionInfo"/> describes.
/// </summary>
/// <value>
The name of the file described by this instance of <see cref="FileVersionInfo"/>.</value>
public string get
return #endregion


#region
/// <summary>
///
Gets the major part of the version number.
/// </summary>
/// <value>
A value representing the major part of the version number.</value>
/// <remarks>
Typically, a version number is displayed as "major number.minor number.build number.private part number".
/// A file version number is a 64-bit number that holds the version number for a file as follows:
/// <list type="bullet"><item>The first 16 bits are the <see cref="FileMajorPart"/> number.</item>
/// <item>
The next 16 bits are the <see cref="FileMinorPart"/> number.</item>
/// <item>
The third set of 16 bits are the <see cref="FileBuildPart"/> number.</item>
/// <item>
The last 16 bits are the <see cref="FilePrivatePart"/> number.</item></list>
///
This property gets the first set of 16 bits.</remarks>
public int get
return ConvertBitConverter#endregion

#region
/// <summary>
///
Gets the minor part of the version number.
/// </summary>
/// <value>
A value representing the minor part of the version number of the file.</value>
/// <remarks>
Typically, a version number is displayed as "major number.minor number.build number.private part number".
/// A file version number is a 64-bit number that holds the version number for a file as follows:
/// <list type="bullet"><item>The first 16 bits are the <see cref="FileMajorPart"/> number.</item>
/// <item>
The next 16 bits are the <see cref="FileMinorPart"/> number.</item>
/// <item>
The third set of 16 bits are the <see cref="FileBuildPart"/> number.</item>
/// <item>
The last 16 bits are the <see cref="FilePrivatePart"/> number.</item></list>
///
This property gets the second set of 16 bits.</remarks>
public int get
return ConvertBitConverter#endregion

#region
/// <summary>
///
Gets the build number of the file.
/// </summary>
/// <value>
A value representing the build number of the file.</value>
/// <remarks>
Typically, a version number is displayed as "major number.minor number.build number.private part number".
/// A file version number is a 64-bit number that holds the version number for a file as follows:
/// <list type="bullet"><item>The first 16 bits are the <see cref="FileMajorPart"/> number.</item>
/// <item>
The next 16 bits are the <see cref="FileMinorPart"/> number.</item>
/// <item>
The third set of 16 bits are the <see cref="FileBuildPart"/> number.</item>
/// <item>
The last 16 bits are the <see cref="FilePrivatePart"/> number.</item></list>
///
This property gets the third set of 16 bits.</remarks>
public int get
return ConvertBitConverter#endregion

#region
/// <summary>
///
Gets the file private part number.
/// </summary>
/// <value>
A value representing the file private part number.</value>
/// <remarks>
Typically, a version number is displayed as "major number.minor number.build number.private part number".
/// A file version number is a 64-bit number that holds the version number for a file as follows:
/// <list type="bullet"><item>The first 16 bits are the <see cref="FileMajorPart"/> number.</item>
/// <item>
The next 16 bits are the <see cref="FileMinorPart"/> number.</item>
/// <item>
The third set of 16 bits are the <see cref="FileBuildPart"/> number.</item>
/// <item>
The last 16 bits are the <see cref="FilePrivatePart"/> number.</item></list>
///
This property gets the last set of 16 bits.</remarks>
public int get
return ConvertBitConverter#endregion


#region
/// <summary>
///
Gets the major part of the version number for the product this file is associated with.
/// </summary>
/// <value>
A value representing the major part of the product version number.</value>
/// <remarks>
Typically, a version number is displayed as "major number.minor number.build number.private part number".
/// A product version number is a 64-bit number that holds the version number for a product as follows:
/// <list type="bullet"><item>The first 16 bits are the <see cref="ProductMajorPart"/> number.</item>
/// <item>
The next 16 bits are the <see cref="ProductMinorPart"/> number.</item>
/// <item>
The third set of 16 bits are the <see cref="ProductBuildPart"/> number.</item>
/// <item>
The last 16 bits are the <see cref="ProductPrivatePart"/> number.</item></list>
///
This property gets the first set of 16 bits.</remarks>
public int get
return ConvertBitConverter#endregion

#region
/// <summary>
///
Gets the minor part of the version number for the product the file is associated with.
/// </summary>
/// <value>
A value representing the minor part of the product version number.</value>
/// <remarks>
Typically, a version number is displayed as "major number.minor number.build number.private part number".
/// A product version number is a 64-bit number that holds the version number for a product as follows:
/// <list type="bullet"><item>The first 16 bits are the <see cref="ProductMajorPart"/> number.</item>
/// <item>
The next 16 bits are the <see cref="ProductMinorPart"/> number.</item>
/// <item>
The third set of 16 bits are the <see cref="ProductBuildPart"/> number.</item>
/// <item>
The last 16 bits are the <see cref="ProductPrivatePart"/> number.</item></list>
///
This property gets the second set of 16 bits.</remarks>
public int get
return ConvertBitConverter#endregion

#region
/// <summary>
///
Gets the build number of the product this file is associated with.
/// </summary>
/// <value>
A value representing the build part of the product version number.</value>
/// <remarks>
Typically, a version number is displayed as "major number.minor number.build number.private part number".
/// A product version number is a 64-bit number that holds the version number for a product as follows:
/// <list type="bullet"><item>The first 16 bits are the <see cref="ProductMajorPart"/> number.</item>
/// <item>
The next 16 bits are the <see cref="ProductMinorPart"/> number.</item>
/// <item>
The third set of 16 bits are the <see cref="ProductBuildPart"/> number.</item>
/// <item>
The last 16 bits are the <see cref="ProductPrivatePart"/> number.</item></list>
///
This property gets the third set of 16 bits.</remarks>
public int get
return ConvertBitConverter#endregion

#region
/// <summary>
///
Gets the private part number of the product this file is associated with.
/// </summary>
/// <value>
A value representing the private part of the product version number.</value>
/// <remarks>
Typically, a version number is displayed as "major number.minor number.build number.private part number".
/// A product version number is a 64-bit number that holds the version number for a product as follows:
/// <list type="bullet"><item>The first 16 bits are the <see cref="ProductMajorPart"/> number.</item>
/// <item>
The next 16 bits are the <see cref="ProductMinorPart"/> number.</item>
/// <item>
The third set of 16 bits are the <see cref="ProductBuildPart"/> number.</item>
/// <item>
The last 16 bits are the <see cref="ProductPrivatePart"/> number.</item></list>
///
This property gets the last set of 16 bits.</remarks>
public int get
return ConvertBitConverter#endregion


#region
#endregion

#region
#endregion

#region
#endregion

#region
#endregion

#region
#endregion

#region
DllImport"coredll""GetFileVersionInfo"trueprivate static extern bool string int int IntPtr DllImport"coredll""GetFileVersionInfoSize"trueprivate static extern int string ref int DllImport"coredll""VerQueryValue"trueprivate static extern bool IntPtr string ref IntPtr ref int DllImport"coredll.dll""LocalAlloc"truestatic extern IntPtr uint uint DllImport"coredll.dll""LocalFree"truestatic extern IntPtr IntPtr #endregion

}


本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2009/07/29/1533758.html,如需转载请自行联系原作者



相关文章
|
7月前
|
Java 数据建模 编译器
JDK21新特性Record Patterns记录模式详解
JDK21新特性Record Patterns记录模式详解
85 2
|
7月前
|
C# Windows
[记录]c#.net framework 4.5调用运行时库
[记录]c#.net framework 4.5调用运行时库
为什么 SAP Spartacus 4.0 源代码版本读取不到 CX_BASE_URL 环境变量的值?
为什么 SAP Spartacus 4.0 源代码版本读取不到 CX_BASE_URL 环境变量的值?
100 0
为什么 SAP Spartacus 4.0 源代码版本读取不到 CX_BASE_URL 环境变量的值?
|
开发框架 并行计算 .NET
c1xx : warning C4199: C++/CLI、C++/CX 或 OpenMP 不支持两阶段名称查找;请使用 /Zc:twoPhase-
c1xx : warning C4199: C++/CLI、C++/CX 或 OpenMP 不支持两阶段名称查找;请使用 /Zc:twoPhase-
1202 0