FileVersionInfo.GetVersionInfo出现意外行为

时间:2013-01-25 13:11:20

标签: c# .net powershell

为什么System.Diagnostics.FileVersionInfo.GetVersionInfo()会返回意外的文件版本信息?我正在寻找有关MPIO驱动程序的版本信息。目标操作系统是Server 2008R2 SP1,它应该返回FileVersion 6.1.7601。取而代之的是,我获得了6.1R600的2008R2 RTM版本。

除了错误的文件版本之外,OriginalFilename也不是我所期望的。它是mpio.sys.mui,虽然FileName是正确的。

使用资源管理器检查文件属性时,会显示正确的版本信息。

这是设计,错误还是我使用FileVersionInfo错误的方式?是否有任何变通办法,最好是在Powershell上?

$mpioPath = 'c:\windows\system32\drivers\mpio.sys'
$v = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($mpioPath)
$v | fl -Property *

Comments           :
CompanyName        : Microsoft Corporation
FileBuildPart      : 7601
FileDescription    : MultiPath Support Bus-Driver
FileMajorPart      : 6
FileMinorPart      : 1
FileName           : c:\windows\system32\drivers\mpio.sys
FilePrivatePart    : 17619
FileVersion        : 6.1.7600.16385 (win7_rtm.090713-1255)
InternalName       : mpio.sys
IsDebug            : False
IsPatched          : False
IsPrivateBuild     : False
IsPreRelease       : False
IsSpecialBuild     : False
Language           : English (United States)
LegalCopyright     : © Microsoft Corporation. All rights reserved.
LegalTrademarks    :
OriginalFilename   : mpio.sys.mui
PrivateBuild       :
ProductBuildPart   : 7601
ProductMajorPart   : 6
ProductMinorPart   : 1
ProductName        : Microsoft® Windows® Operating System
ProductPrivatePart : 17619
ProductVersion     : 6.1.7600.16385
SpecialBuild       :

使用C#程序可以获得相同的结果,因此这似乎更多是.Net功能而不是Powershell特定的功能。

namespace Foo {
  class GetFileVersionInfo {
    static void Main(string[] args) {
      string mpio = @"c:\windows\system32\drivers\mpio.sys";
      System.Diagnostics.FileVersionInfo fvInfo;
      fvInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(mpio);
      System.Console.WriteLine("Original file name: " + fvInfo.OriginalFilename);
      System.Console.WriteLine("FileVersion: " + fvInfo.FileVersion);
    }
  }
}

使用FileVer.exe返回正确的版本信息:

filever $mpioPath
--a-- W32  DRV ENU  6.1.7601.17619 shp    156,544 05-20-2011 mpio.sys

我可以使用FileVer并解析其输出,如果没有其他工作。

3 个答案:

答案 0 :(得分:2)

我猜FileVer.exe和explorer.exe的做法与您在powershell中的做法相同:

$mpioPath = 'c:\windows\system32\drivers\mpio.sys'
$v = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($mpioPath)

$ver = "{0}.{1}.{2}.{3}" -f $v.FileMajorPart, $v.FileMinorPart, $v.FileBuildPart, $v.FilePrivatePart

答案 1 :(得分:2)

MSDN page for GetFileVersionInfo它说:

  

文件版本信息具有固定和非固定部分。固定部分包含版本号等信息。非固定部分包含字符串之类的东西。在过去,GetFileVersionInfo正在从二进制文件(exe / dll)中获取版本信息。目前,它从语言中性文件(exe / dll)查询固定版本,从mui文件查询非固定部分,合并它们并返回给用户。

因此,这与您所看到的完全一致:一个版本号来自c:\windows\system32\drivers\mpio.sys,另一个来自c:\windows\system32\drivers\[your language]\mpio.sys.mui

答案 2 :(得分:1)

据我所知,字段“FileVersion”和“ProductVersion”是unicode 字符串。相比之下,字段“FileMajorPart”,“FileMinorPart”,“FileBuildPart”和“FilePrivatePart”是 DWORD值。 “ProductMajorPart”,“ProductMinorPart”,“ProductBuildPart”和“ProductPrivatePart”字段也是DWORD值:

http://msdn.microsoft.com/en-us/library/windows/desktop/ms646997%28v=vs.85%29.aspx

用于创建VersionBlock的应用程序可能允许字符串和DWORD字段之间的不一致。例如,某些版本的Visual Studio将始终更新DWORD字段以反映对字符串所做的更改。但是,仅DWORD值的更新不会反映在字符串中。因此,取决于用于编码的应用可能存在不同程度的不一致。根据我的经验,您将仅使用DWORD字段获得最佳结果(正如您在问题的答案中提出的那样)。

相关问题