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 System;
using System.Collections;
using System.Runtime.InteropServices;

namespace OpenNETCF.Diagnostics
{
/// <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 m_filename;

//the language independant version info
private byte[] m_fixedversioninfo;

private const int GMEM_FIXED = 0x0000;
private const int LMEM_ZEROINIT = 0x0040;
private const int LPTR = (GMEM_FIXED | LMEM_ZEROINIT);

#region Constructor
private FileVersionInfo(string fileName)
{
//get the filename sans path
m_filename = System.IO.Path.GetFileName(fileName);

int handle = 0;
int len = 0;

//get size of version info
len = GetFileVersionInfoSize(fileName, ref handle);

if(len > 0)
{
//allocate buffer
//IntPtr buffer = LocalAlloc(LPTR, (uint)0xffff);
IntPtr buffer = LocalAlloc(LPTR, (uint)len);
//IntPtr buffer = MarshalEx.AllocHGlobal(len);
//get version information
if(GetFileVersionInfo(fileName, handle, len, buffer))
{
IntPtr fixedbuffer = IntPtr.Zero;
int fixedlen = 0;
//get language independant version info
//this is a pointer within the main buffer so don't free it
if(VerQueryValue(buffer, "\\", ref fixedbuffer, ref fixedlen))
{
//allocate managed memory
m_fixedversioninfo = new byte[fixedlen];
//copy to managed memory
Marshal.Copy(fixedbuffer, m_fixedversioninfo, 0, fixedlen);
}
else
{
throw new Exception("Error retrieving language independant version");
}
}
else
{
throw new Exception("Error retrieving FileVersionInformation");
}

//free native buffer
//MarshalEx.FreeHGlobal(buffer);
if (buffer != IntPtr.Zero)
{
LocalFree(buffer);
buffer = IntPtr.Zero;
}

}

}
#endregion

#region
Get Version Info
/// <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 GetVersionInfo(string fileName)
{
//check if file exists first
if(System.IO.File.Exists(fileName))
{
return new FileVersionInfo(fileName);
}
else
{
throw new System.IO.FileNotFoundException("The specified file was not found");
}
}
#endregion

#region
File Name
/// <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 FileName
{
get
{
return m_filename;
}
}
#endregion


#region
FileMajorPart
/// <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 FileMajorPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_fixedversioninfo, 10));
}
}
#endregion

#region
FileMinorPart
/// <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 FileMinorPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_fixedversioninfo, 8));
}
}
#endregion

#region
FileBuildPart
/// <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 FileBuildPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_fixedversioninfo, 14));
}
}
#endregion

#region
FilePrivatePart
/// <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 FilePrivatePart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_fixedversioninfo, 12));
}
}
#endregion


#region
ProductMajorPart
/// <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 ProductMajorPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_fixedversioninfo, 18));
}
}
#endregion

#region
ProductMinorPart
/// <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 ProductMinorPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_fixedversioninfo, 16));
}
}
#endregion

#region
ProductBuildPart
/// <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 ProductBuildPart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_fixedversioninfo, 22));
}
}
#endregion

#region
ProductPrivatePart
/// <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 ProductPrivatePart
{
get
{
return Convert.ToInt32(BitConverter.ToInt16(m_fixedversioninfo, 20));
}
}
#endregion


#region
IsDebug
#endregion

#region
IsPatched
#endregion

#region
IsPreRelease
#endregion

#region
IsPrivateBuild
#endregion

#region
IsSpecialBuild
#endregion

#region
P/Invokes

[DllImport("coredll", EntryPoint="GetFileVersionInfo", SetLastError=true)]
private static extern bool GetFileVersionInfo(string filename, int handle, int len, IntPtr buffer);

[DllImport("coredll", EntryPoint="GetFileVersionInfoSize", SetLastError=true)]
private static extern int GetFileVersionInfoSize(string filename, ref int handle);

[DllImport("coredll", EntryPoint="VerQueryValue", SetLastError=true)]
private static extern bool VerQueryValue(IntPtr buffer, string subblock, ref IntPtr blockbuffer, ref int len);

[DllImport("coredll.dll", EntryPoint = "LocalAlloc", SetLastError = true)]
static extern IntPtr LocalAlloc(uint uFlags, uint Bytes);

[DllImport("coredll.dll", EntryPoint = "LocalFree", SetLastError = true)]
static extern IntPtr LocalFree(IntPtr hMem);

#endregion
}
}
posted @ 2009-07-29 10:05  Jake Lin  阅读(1381)  评论(1编辑  收藏  举报