动态获取非托管的dll文件的版本号

解决方案 »

  1.   


    [DllImport("version.dll")]
     static extern public bool GetFileVersionInfo(string fileName, uint handle, uint verLen, byte[] data);[DllImport("version.dll")]
    static extern public uint GetFileVersionInfoSize(string fileName, ref uint handle);
    [DllImport("version.dll")]
    unsafe static extern public bool VerQueryValue(byte[] data, string sub, byte** subData, ref uint subLen);
      

  2.   

    解决了,根据楼上提供的代码查阅了MSDN文档下面是具体实现的代码:public class Win32Imports 
    {
          [DllImport("version.dll")]
          public static extern bool GetFileVersionInfo (string sFileName,
                int handle, int size, byte[] infoBuffer);
          [DllImport("version.dll")]
          public static extern int GetFileVersionInfoSize (string sFileName,
                out int handle);
       
          // The third parameter - "out string pValue" - is automatically
          // marshaled from ANSI to Unicode:
          [DllImport("version.dll")]
          unsafe public static extern bool VerQueryValue (byte[] pBlock,
                string pSubBlock, out string pValue, out uint len);
          // This VerQueryValue overload is ed with 'unsafe' because 
          // it uses a short*:
          [DllImport("version.dll")]
          unsafe public static extern bool VerQueryValue (byte[] pBlock,
                string pSubBlock, out short *pValue, out uint len);
    }
     
    public class C 
    {
          // Main is ed with 'unsafe' because it uses pointers:
          unsafe public static int Main () 
          {
                try 
                {
                      int handle = 0;
                      // Figure out how much version info there is:
                      int size =
                            Win32Imports.GetFileVersionInfoSize("printversion.exe",
                            out handle);
     
                      if (size == 0) return -1;
     
                      byte[] buffer = new byte[size];
     
                      if (!Win32Imports.GetFileVersionInfo("printversion.exe", handle, size, buffer))
                      {
                            Console.WriteLine("Failed to query file version information.");
                            return 1;
                      }
     
                      short *subBlock = null;
                      uint len = 0;
                      // Get the locale info from the version info:
                      if (!Win32Imports.VerQueryValue (buffer, @"\VarFileInfo\Translation", out subBlock, out len))
                      {
                            Console.WriteLine("Failed to query version information.");
                            return 1;
                      }
     
                      string spv = @"\StringFileInfo\" + subBlock[0].ToString("X4") + subBlock[1].ToString("X4") + @"\ProductVersion";
     
                      byte *pVersion = null;
                      // Get the ProductVersion value for this program:
                      string versionInfo;                  if (!Win32Imports.VerQueryValue (buffer, spv, out versionInfo, out len))
                      {
                            Console.WriteLine("Failed to query version information.");
                            return 1;
                      }
     
                     Console.WriteLine ("ProductVersion == {0}", versionInfo);
                }
                catch (Exception e) 
                {
                      Console.WriteLine ("Caught unexpected exception " + e.Message);
                }
          
                return 0;
          }
    }
    输出示例:
    ProductVersion == 4.3.2.1
      

  3.   

    系统提供的API也可以获取托管dll的版本号