System: Windows 7 Professional 64 bit, PowerShell v 2.0, no Visual Studio (can't be installed and others too)
Trying to run PowerShell from C#. This is the code snippet:
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);
}
}
}
Error:
e:\\foo.cs(2,25): error CS0234: The type or namespace name 'Automation' does not exist in the namespace 'System.Management' (are you missing an assembly reference?)
Since no Visual Studio is there, I am running code in raw manner. Because of error, I downloaded dll from http://www.dll-found.com/system.management.automation.dll_download.html
and placed in dir as per instruction. After rebooting machine, there was no success.
First, I want to ask a general question. How to install missing assembly or dll file (only), because for some you might have to install whole Windows or PowerShell SDK or .NET Framework.
EDIT
I have place downloaded dll file in C:\\Windows\\SysWOW64, C:\\Windows\\system32, C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.5 and C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0.
I am compiling using: C:\\Windows\\Microsoft.NET\\Framework\\v3.5\\csc.exe /target:exe /out:E:\\foo.exe E:\\foo.cs
NuGet worked for me.
PM> Install-Package System.Management.Automation.dll -Version 10.0.10586
您必须对csc.exe
使用/reference
命令行参数,如MSDN 中所述:
C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /reference:system.management.automation.dll /target:exe /out:E:\foo.exe /E:\foo.cs
If you have this problem after re-targeting an old project to, for example, .NET 4.8, then it is because NuGet package names have changed.
System.Management.Automation
Microsoft.PowerShell.5.1.ReferenceAssemblies
( NuGet ) This package name appears in the documentation of classes in the namespace System.Management.Automation
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.