C# FileVersionInfo.GetVersionInfo出现意外行为

C# FileVersionInfo.GetVersionInfo出现意外行为,c#,.net,powershell,C#,.net,Powershell,为什么System.Diagnostics.FileVersionInfo.GetVersionInfo()会返回意外的文件版本信息?我正在查找有关MPIO驱动程序的版本信息。目标操作系统是服务器2008R2 SP1,它应该返回文件版本6.1.7601。相反,我得到了6.1.7600的2008R2 RTM版本 除了不正确的文件版本,原始文件名也不是我所期望的。它是mpio.sys.mui,但文件名是正确的 使用资源管理器检查文件属性时,将显示正确的版本信息 这是出于设计,还是我在使用FileVe

为什么
System.Diagnostics.FileVersionInfo.GetVersionInfo()
会返回意外的文件版本信息?我正在查找有关MPIO驱动程序的版本信息。目标操作系统是服务器2008R2 SP1,它应该返回文件版本6.1.7601。相反,我得到了6.1.7600的2008R2 RTM版本

除了不正确的文件版本,原始文件名也不是我所期望的。它是mpio.sys.mui,但文件名是正确的

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

这是出于设计,还是我在使用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并解析其输出。

我猜FileVer.exe和expolr.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
据我所知,“FileVersion”和“ProductVersion”字段是unicode字符串。相反,“FileMajorPart”、“FileMinorPart”、“FileBuildPart”和“FilePrivatePart”字段是DWORD值。“ProductMajorPart”、“ProductMinorPart”、“ProductBuildPart”和“ProductPrivatePart”字段也是DWORD值:

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

最后,它说:

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


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

我接受这一答案,尽管它没有解释版本信息不一致的原因。