以编程方式启动独立于平台的进程
Programmatically start a process independent of platform
情况
我正在尝试以编程方式运行 命令行工具DISM.exe。当我 运行 它手动工作时,但是当我尝试使用以下命令生成它时:
var systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
var dism = new Process();
dism.StartInfo.FileName = Path.Combine(systemPath, "Dism.exe");
dism.StartInfo.Arguments = "/Online /Get-Features /Format:Table";
dism.StartInfo.Verb = "runas";
dism.StartInfo.UseShellExecute = false;
dism.StartInfo.RedirectStandardOutput = true;
dism.Start();
var result = dism.StandardOutput.ReadToEnd();
dism.WaitForExit();
然后我的 result
结果是:
Error: 11
You cannot service a running 64-bit operating system with a 32-bit version of DISM.
Please use the version of DISM that corresponds to your computer's architecture.
问题
实际上我已经知道是什么原因造成的:我的项目设置为针对 x86 平台进行编译。 (例如,参见 this question,尽管 none 的答案提到了这一点)。然而,不幸的是,目前我们继续以这个平台为目标是一项要求,我 不能 能够通过切换到 Any CPU.
来解决这个问题
所以我的问题是如何以独立于其父平台的方式以编程方式生成一个进程,即保持我的项目以 x86 为目标,但启动一个进程,该进程将以其所在机器的正确平台为目标上。
虽然它没有回答您关于从 32 位启动 64 位进程的问题,但解决您的潜在问题的另一种方法是查询 WMI 以获取您需要的信息。你可以list optional features or list Server Features
This answer 提供有关从 C# 执行 WMI 查询的一般信息。
您也可以 check and install windows features from powershell,您可以从您的程序中生成它,而不用启动 DISM。
even though I'm running the correct DSIM.exe in System32
但你不是。这才是重点。 The file system redirector 属于 32 位进程,因此当您从 x86
进程请求 System32
时,您实际上是从 SysWow64
获取文件。如果你想访问 64 位 exe,你需要通过 %windir%\sysnative
请求它
(%windir%
为 SpecialFolder.Windows
)
情况
我正在尝试以编程方式运行 命令行工具DISM.exe。当我 运行 它手动工作时,但是当我尝试使用以下命令生成它时:
var systemPath = Environment.GetFolderPath(Environment.SpecialFolder.System);
var dism = new Process();
dism.StartInfo.FileName = Path.Combine(systemPath, "Dism.exe");
dism.StartInfo.Arguments = "/Online /Get-Features /Format:Table";
dism.StartInfo.Verb = "runas";
dism.StartInfo.UseShellExecute = false;
dism.StartInfo.RedirectStandardOutput = true;
dism.Start();
var result = dism.StandardOutput.ReadToEnd();
dism.WaitForExit();
然后我的 result
结果是:
Error: 11
You cannot service a running 64-bit operating system with a 32-bit version of DISM. Please use the version of DISM that corresponds to your computer's architecture.
问题
实际上我已经知道是什么原因造成的:我的项目设置为针对 x86 平台进行编译。 (例如,参见 this question,尽管 none 的答案提到了这一点)。然而,不幸的是,目前我们继续以这个平台为目标是一项要求,我 不能 能够通过切换到 Any CPU.
来解决这个问题所以我的问题是如何以独立于其父平台的方式以编程方式生成一个进程,即保持我的项目以 x86 为目标,但启动一个进程,该进程将以其所在机器的正确平台为目标上。
虽然它没有回答您关于从 32 位启动 64 位进程的问题,但解决您的潜在问题的另一种方法是查询 WMI 以获取您需要的信息。你可以list optional features or list Server Features
This answer 提供有关从 C# 执行 WMI 查询的一般信息。
您也可以 check and install windows features from powershell,您可以从您的程序中生成它,而不用启动 DISM。
even though I'm running the correct DSIM.exe in System32
但你不是。这才是重点。 The file system redirector 属于 32 位进程,因此当您从 x86
进程请求 System32
时,您实际上是从 SysWow64
获取文件。如果你想访问 64 位 exe,你需要通过 %windir%\sysnative
(%windir%
为 SpecialFolder.Windows
)