2

我正在尝试使用 powershell 来获取文件的文件版本。如果我右键单击文件并查看版本,它会显示一个值。这是我尝试的方法:

$path = "MSDE2000A";
$info = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($path);

这是它抛出的异常信息:

Exception calling "GetVersionInfo" with "1" argument(s): "MSDE2000A.exe"
At line:1 char:58
+ $f = [system.diagnostics.fileversioninfo]::getversioninfo <<<< ("MSDE2000A.exe")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

我检查过的每个文件都有相同的结果。但是,如果我的路径是 c:\windows\notepad.exe (如示例中所示),它会按预期工作。这是怎么回事?

4

1 回答 1

7

.NET 和 PowerShell 的当前目录概念并不总是相同的。尝试传入绝对路径。

[Diagnostics.FileVersionInfo]::GetVersionInfo('C:\Windows\System32\user32.dll')

ProductVersion   FileVersion      FileName
--------------   -----------      --------
6.1.7600.16385   6.1.7600.1638... C:\Windows\System32\user32.dll

此外,您可以使用 Get-ChildItem 获取此信息,如下所示:

Get-ChildItem C:\Windows\System32\user32.dll | fl VersionInfo
于 2009-12-16T22:02:04.243 回答