我有一个 .NET 4.0 C# 项目,我需要在 powershell 运行空间中通过 C# 运行 Powershell。我已经测试了我的代码,一切正常。
在新计算机(Win10、VS2017)上打开同一个项目并尝试运行同一个项目时出现问题。 我在以下位置引用了 DLL:
C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
Also doing this using commands
using System.Management;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
但无法识别 System.Management.Automation 和 System.Management.Automation.Runspaces。
出现错误:命名空间“System.Management”中不存在类型或命名空间名称“Automation”(是否缺少程序集引用?)
不知何故,引用没有被使用...
最佳答案
不确定到底发生了什么,但是当我引用以下 DLL 时:
C:\windows\Microsoft.Net\assembly\GAC_MSIL\System.Management.Automation\v4.0_3.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
VS 忽略了这个 DLL 并提示我们缺少 Automation 命名空间...虽然这个 DLL 显示为 .NET 4.0,但显然它是用更高版本的 .NET 编译的,这可以解释为什么它被忽略。
为了解决这个问题,我必须通过浏览并选择这个 DLL 来添加一个引用:
C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll
这看起来像是旧版本的 powershell 引擎。
可能导致此问题的另一个注意事项是,在我的新 Win10 机器上,我安装了 VS2017,因此我没有手动安装 .NET 4.0 FW,也许 VS2017 安装放置了更高版本的 System.Management.Automation DLL ...
关于c# - 对 C# .NET 4.0 上 Powershell 的 System.Management 的引用不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45327912/