7,655
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 获取托管和非托管文件的版本信息(如:1.0.0.2)
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
[DllImport("version.dll")]
static extern private bool GetFileVersionInfo(string fileName, uint handle, uint verLen, byte[] data);
[DllImport("version.dll")]
static extern private uint GetFileVersionInfoSize(string fileName, out uint handle);
[DllImport("version.dll")]
unsafe static extern private bool VerQueryValue(byte[] data, string sub, out short* subData, out uint subLen);
[DllImport("version.dll")]
private static extern bool VerQueryValue(byte[] pBlock, string pSubBlock, out string pValue, out uint len);
unsafe public static string GetFileVersion(string path)
{
uint handle = 0;
uint size = GetFileVersionInfoSize(path, out handle);
if (size != 0)
{
byte[] buffer = new byte[size];
if (GetFileVersionInfo(path, handle, size, buffer))
{
short* subBlock = null;
uint len = 0;
if (VerQueryValue(buffer, @"\VarFileInfo\Translation", out subBlock, out len))
{
string spv = @"\StringFileInfo\" + subBlock[0].ToString("X4") + subBlock[1].ToString("X4") + @"\ProductVersion";
string versionInfo;
if (VerQueryValue(buffer, spv, out versionInfo, out len))
{
return versionInfo;
}
}
}
}
return null;
}
Assembly assembly = Assembly.LoadFrom(path);
AssemblyName assemblyName = assembly.GetName();
Version version = assemblyName.Version;