Click here to Skip to main content
15,885,079 members
Articles / Programming Languages / C#

VersionInfo Parser

Rate me:
Please Sign up or sign in to vote.
2.33/5 (2 votes)
26 Aug 2007CPOL2 min read 25.6K   597   8  
A simple parser for VERSIONINFO resources contained in a DLL or EXE.

Introduction

The FileVersionInfo class from System.Diagnostics has a major drawback in that it does not allow accessing information by name. If the name of the information to request is known, we could use the Win32 VerQueryValue function. But there is no way to enumerate all the information contained in a resource by using the APIs provided by .NET or Win32.

Initially, I set out to implement a drop-in replacement for the .NET FileVersionInfo class that would allow me to access all the information in a resource. Unfortunately, this task proved to be too difficult, and I decided to implement the class as a push parser. This allowed me to separate the ugly internals of the version information format from the code that tries to make sense of the content. Internally, the class uses P/Invoke on GetFileVersionInfo to get at the information.

Using the Code

The code below shows how the parser is used:

C#
VersionInfoParser.Parse(fileName, delegate(ParseInfo info)
{
  Console.WriteLine("{0}\t: {1}", info.Key, info.Value);
});

The parser calls the delegate for each visited node in the resource. The sample project contains a small console application that shows what type of key/value pairs are to be expected and how they can be used to format the output.

The following shows how the sample application formats the output:

Versioninfo for file 'C:\windows\system32\ctl3d32.dll'

VS_VERSION_INFO
 VS_FIXEDFILEINFO
  FileVersion      : 2.31.0.0
  ProductVersion   : 2.31.0.0
  FileFlagsMask    : ALL
  FileFlags        : VS_FF_PRERELEASE, VS_FF_PRIVATEBUILD
  FileOS           : VOS_DOS_WINDOWS16
  FileType         : VFT_DLL
  FileSubtype      : VFT2_UNKNOWN
 StringFileInfo
**Warning: wrong codepage (1252) for 16bit resource
  StringTable
   Language        : 1033
   CodePage        : 1252
   CompanyName     : Microsoft Corporation
   FileDescription : Ctl3D 3D Windows Controls
   FileVersion     : 2.31.000
   InternalName    : CTL3D32
   LegalCopyright  : Copyright c Microsoft Corp.
   ProductName     : 3D Windows Controls
   ProductVersion  : 2,31,0,0
 VarFileInfo
  Translation
   Language        : 1033
   CodePage        : 1252

Points of Interest

What surprised me most with version resources is that there are lots of files around that have malformed content. (Like \0 characters in strings, unknown/illegal codepages and languages, etc.). I initially started with lots of checks that threw an exception if something was wrong, but soon realized that this would not give me a useful program. That's why I now have the "?" tag that I can use to display a warning and keep on parsing.

Another issue that baffled me was the lack of Unicode support of cmd.exe. If anyone knows how to properly display Unicode content in cmd.exe, I'd be happy to hear about it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --