Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DLL Version property?

24 views
Skip to first unread message

msnews.microsoft.com

unread,
Sep 20, 2001, 9:54:21 PM9/20/01
to
I have been searching for a while here and cannot find what I need.

Can anyone tell me how to grab a dll version using C#? I need to scan all
dll files in a directory and load their version numbers (and hopefully,
other property collection info) into a database.

I'm sure it's in the libs somewhere, but I can't find it so far ...

Thanks in advance ! jf


east spider

unread,
Sep 20, 2001, 11:34:40 PM9/20/01
to
hope this helps

// printversion.cs
// compile with: /unsafe
using System;
using System.Reflection;
using System.Runtime.InteropServices;

// Give this assembly a version number:
[assembly:AssemblyVersion("4.3.2.1")]

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 3rd parameter - "out string pValue" - is automatically
// marshaled from Ansi to Unicode:
[DllImport("version.dll")]
unsafe public static extern int VerQueryValue (byte[] pBlock,
string pSubBlock, out string pValue, out uint len);
// This VerQueryValue overload is marked with 'unsafe' because
// it uses a short*:
[DllImport("version.dll")]
unsafe public static extern int VerQueryValue (byte[] pBlock,
string pSubBlock, out short *pValue, out uint len);
}

public class C
{
// Main is marked 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);
// Be sure to allocate a little extra space for safety:
byte[] buffer = new byte[size+2];
Win32Imports.GetFileVersionInfo ("printversion.exe",
handle, size, buffer);
short *subBlock = null;
uint len = 0;
// Get the locale info from the version info:
Win32Imports.VerQueryValue (buffer,
"\\VarFileInfo\\Translation", out subBlock, out len);
string spv =
"\\StringFileInfo\\" + subBlock[0].ToString("X4")
+ subBlock[1].ToString("X4") + "\\ProductVersion";
byte *pVersion = null;
// Get the ProductVersion value for this program:
string versionInfo;
Win32Imports.VerQueryValue (buffer, spv,
out versionInfo, out len);
Console.WriteLine ("ProductVersion == {0}", versionInfo);
}
catch (Exception e)
{
Console.WriteLine ("Caught unexpected exception " +
e.ToString() + "\n\n" + e.Message);
}

return 0;
}
}
Example Output
ProductVersion == 4.3.2.1

"msnews.microsoft.com" <jfroh_NOSPAM_@_NOSPAM_earthlink.net> 写入消息新闻
:O0Os89jQBHA.1468@tkmsftngp03...

0 new messages