简体   繁体   English

如何使用C#在x64或x86中运行PowerShell?

[英]How to run PowerShell in x64 or x86 using C#?

I'm using this C# code to read my installed programs using PowerShell. 我正在使用这个C#代码来使用PowerShell读取我安装的程序。

I need it to read both x64 and x86 registry through PowerShell, how am I doing this? 我需要它通过PowerShell读取x64和x86注册表,我是怎么做到的?

Is there a way to redirect? 有没有办法重定向? or maybe run PowerShell in x64 and then x86? 或者可以在x64中运行PowerShell然后再运行x86?

public void conf() {
process p1 = new Process();
ProcessStartInfo psi1 = new ProcessStartInfo("powershell", "Get-ItemProperty HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* | Select-Object DisplayName");
psi1.RedirectStandardOutput = true;
psi1.CreateNoWindow = true;
p1.StartInfo = psi1;
p1.StartInfo.UseShellExecute = false;
p1.StartInfo.Verb = "runas";
p1.Start();
string output = p1.StandardOutput.ReadToEnd();
Console.WriteLine(output);
p1.WaitForExit(400);
}

This should do if your process is running in x64 (or it's an x86 process running on an x86 OS). 如果您的进程在x64中运行(或者它是在x86 OS上运行的x86进程),则应该这样做。

bool is64 = IntPtr.Size == 8;
var cmdline = "Get-ItemProperty HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\* " 
       + (is64 ? ",HKLM:\\Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\*" : "") 
       + " | Select-Object DisplayName";
ProcessStartInfo psi1 = new ProcessStartInfo("powershell", cmdline);

This won't work if the process is a 32-bit process running on an x64 OS, but for .NET, it should work with AnyCPU as long as you don't choose "prefer 32-bit" 如果进程是在x64操作系统上运行的32位进程,这将不起作用,但对于.NET,只要您不选择“prefer 32-bit”,它就可以与AnyCPU一起使用

Update 更新

If you are only aiming at getting the display names, there might be "display-name duplicates" (in both registry keys)... so you can just remove them from the output. 如果您的目的只是获取显示名称,则可能存在“显示名称重复”(在两个注册表项中)...因此您可以从输出中删除它们。 This will remove duplicates and sort: 这将删除重复项并排序:

var result = new StringBuilder();
var resultArr = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Distinct().ToArray();
Array.Sort(resultArr, StringComparer.InvariantCulture);
foreach (string s in resultArr)
    result.AppendLine(s);
output = result.ToString(); 

Update 2 更新2

If you don't want to mangle with processes and capturing output, you can install the System.Management.Automation nuget package and use powershell directly. 如果您不想使用进程和捕获输出进行修改,可以安装System.Management.Automation nuget包并直接使用powershell。

The whole equivalent program would be: 整个等效程序将是:

PowerShell ps = PowerShell.Create();
ps.AddCommand("Get-ItemProperty");
var parm = new List<string> {
     @"HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*"
};
if(IntPtr.Size == 8)
  parm.Add(@"HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*");
var pso = ps.Invoke(parm);
var result = new StringBuilder();
foreach (var ob in pso)
{
  if(ob.Members["DisplayName"] != null)
    result.AppendLine(ob.Members["DisplayName"].Value.ToString());
}
Console.WriteLine(result.ToString());

This should be better than calling the process :-) 这应该比调用过程更好:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用宏的情况下使C#结构与x86和x64兼容? - how to make a C# struct to be compatible with both x86 and x64 without using macro? C# 中的 x86/x64 CPUID - x86/x64 CPUID in C# C# 支持 x64 和 x86 - C# support both x64 and x86 从x64 C#应用程序调用x86 PowerShell脚本 - Invoking a x86 PowerShell Script from an x64 C# Application 如何从 c# 中的 x86 应用程序启动 x64 位应用程序 - How to start x64 bit application from x86 application in c# 在C#中,如何以编程方式了解操作系统是x64还是x86 - In C#, how can I know programmatically if the Operating system is x64 or x86 为x86构建的UWP应用是否可以在x64上运行? - Will a UWP app built for x86 run on x64? VisualStudio C#x64,为什么AddReference选项,.NET选项卡指向x86 DLL而不是x64? - VisualStudio C# x64, why AddReference option, .NET tab points to x86 DLL instead of x64? 使用x86 / x64 C API的C#AnyCPU库-打包结构,调用和回调 - C# AnyCPU library using x86/x64 C API - packing structures, calls and callbacks c#visual studio使用目标anycpu构建exe,但在调用进程平台(x86 / x64)上确定其平台(x86 / x64) - c# visual studio build exe with target anycpu but that determines its platform(x86/x64) on the calling process platform(x86/x64)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM