问题
I tried loading PresentationFramework.dll from .NET 4.0 beta2 in PowerShell v2.0. But it fails with the following error.
PS C:\Windows\system32> [Reflection.Assembly]::LoadFile("C:\Windows\Microsoft.NET\Framework\v4.0.21006\WPF\PresentationF ramework.dll")
Exception calling "LoadFile" with "1" argument(s): "This assembly is built by a runtime newer than the currently loaded runtime and cannot be loaded. (Exception from HRESULT: 0x8013101B)" At line:1 char:32 + [Reflection.Assembly]::LoadFile <<<< ("C:\Windows\Microsoft.NET\Framework\v4.0.21006\WPF\PresentationFramework.dll") + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException
How can I load this DLL file within PowerShell 2.0?
回答1:
You can't load it. PowerShell is using the .NET 2.0 CLR, and 4.0 DLL files cannot be loaded.
It might be possible to reconfigure PowerShell to run in the newer CLR or host PowerShell in a .NET 4.0 application, but I wouldn't recommend either of those.
回答2:
Run this code in the administrative mode of PowerShell:
reg add hklm\software\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
reg add hklm\software\wow6432node\microsoft\.netframework /v OnlyUseLatestCLR /t REG_DWORD /d 1
回答3:
I had similar issue on Windows Server 2008 (with PowerShell v2) and I resolved by installing these 2 updates :
Microsoft .NET Framework 4.5.1 https://www.microsoft.com/fr-fr/download/details.aspx?id=40779
PowerShell v4 https://www.microsoft.com/en-US/download/details.aspx?id=40855
来源:https://stackoverflow.com/questions/1940983/loading-a-net-4-0-beta2-assembly-in-powershell-2-0