操作系统: Windows 7 Professional 64位, PowerShell版本为2.0,没有安装Visual Studio (无法安装以及其他原因)
尝试从C#中运行PowerShell。以下是代码片段:
using System;
using System.Management.Automation;
class Hello {
static void Main(string[] args) {
PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-Process");
Console.WriteLine("Process Id");
Console.WriteLine("----------------------------");
foreach (PSObject result in ps.Invoke()) {
Console.WriteLine(
"{0,-24}{1}",
result.Members["ProcessName"].Value,
result.Members["Id"].Value);
}
}
}
错误:
e:\foo.cs(2,25):error CS0234:名称空间'System.Management'中不存在类型或命名空间名称'Automation'(是否缺少程序集引用?)
由于没有Visual Studio,我以裸代码方式运行代码。由于出现错误,我从 http://www.dll-found.com/system.management.automation.dll_download.html
下载了dll文件,并按照说明放置在目录中。重启计算机后,仍然没有成功。
首先,我想问一个通用问题。如何安装缺失的程序集或dll文件(仅限文件),因为对于某些文件,您可能需要安装整个Windows或PowerShell SDK或.NET Framework。
编辑
我已经将下载的dll文件放置在C:\Windows\SysWOW64、C:\Windows\system32、C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5和C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0中。
我使用以下编译器进行编译: C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /target:exe /out:E:\foo.exe E:\foo.cs
csc.exe
,你必须在它的命令行中指定所有引用,而不仅仅是将 "copy" 的 dll 文件复制到同一个目录。 - v.karbovnichy