9.5 Retrieve Version Information for a File


Problem

You want to retrieve file version information such as the publisher of a file, its revision number, associated comments, and so on.

Solution

Use the static GetVersionInfo method of the System.Diagnostics.FileVersionInfo class.

Discussion

The .NET Framework allows you to retrieve file information without resorting to the Windows API. Instead, you simply need to use the FileVersionInfo class and call the GetVersionInfo method with the file name as a parameter. You can then retrieve extensive information through the FileVersionInfo properties.

The FileVersionInfo properties are too numerous to list here, but the following code snippet shows an example of what you might retrieve:

 using System; using System.Diagnostics; public class VersionInfo {     private static void Main(string[] args) {         if (args.Length == 0) {             Console.WriteLine("Please supply a  file name.");             return;         }         FileVersionInfo info = FileVersionInfo.GetVersionInfo(args[0]);         // Display version information.         Console.WriteLine("Checking File: " + info. file name);         Console.WriteLine("Product Name: " + info.ProductName);         Console.WriteLine("Product Version: " + info.ProductVersion);         Console.WriteLine("Company Name: " + info.CompanyName);         Console.WriteLine("File Version: " + info.FileVersion);         Console.WriteLine("File Description: " + info.FileDescription);         Console.WriteLine("Original  file name: " + info.Original file name);         Console.WriteLine("Legal Copyright: " + info.LegalCopyright);         Console.WriteLine("InternalName: " + info.InternalName);         Console.WriteLine("IsDebug: " + info.IsDebug);         Console.WriteLine("IsPatched: " + info.IsPatched);         Console.WriteLine("IsPreRelease: " + info.IsPreRelease);         Console.WriteLine("IsPrivateBuild: " + info.IsPrivateBuild);         Console.WriteLine("IsSpecialBuild: " + info.IsSpecialBuild);                  Console.ReadLine();     } } 

Here's the output this code produces if you run the command VersionInfo c:\windows\explorer.exe :

 Checking File: c:\windows\explorer.exe Product Name: Microsoftr Windowsr Operating System Product Version: 6.00.2600.0000 Company Name: Microsoft Corporation File Version: 6.00.2600.0000 (xpclient.010817-1148) File Description: Windows Explorer Original  file name: EXPLORER.EXE Legal Copyright: c Microsoft Corporation. All rights reserved. InternalName: explorer IsDebug: False IsPatched: False IsPreRelease: False IsPrivateBuild: False IsSpecialBuild: False 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net