[英]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
一起使用
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();
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.