从 System.Diagnostics.Process 调用 dism.exe 失败

发布于 2024-11-06 02:47:32 字数 626 浏览 1 评论 0原文

为了在 Windows 2008 R2 Server(64 位)中启用 Microsoft-Hyper-V 和 Microsoft-Hyper-V-Management,我将 dism.exe 作为进程进行调用。我用过的命令是

Dism.exe /online /Get-FeatureInfo /FeatureName:Microsoft-Hyper-V

Dism.exe /online /Get-FeatureInfo /FeatureName:Microsoft-Hyper-V-管理-客户端

当我从命令行执行此操作时,效果很好,但当我尝试通过代码执行它时,它会失败。我尝试过 C:\Windows\SysWoW64 文件夹下的 64 位版本的 Dism.exe,但也失败了。这是我收到的错误消息,

您无法为正在运行的 64 位提供服务 32位版本的操作系统 DISM 的。请使用以下版本 与您对应的 DISM 计算机的体系结构。

我在这里缺少什么?

更新:SLAks 是对的,但问题是 MS Visual studio Express 版本默认以 x86 为目标,我必须在 .csproj 文件中手动编辑到 AnyCPU 才能使其工作。

For enabling Microsoft-Hyper-V and Microsoft-Hyper-V-Management in Windows 2008 R2 Server(64bit), I'm calling dism.exe as a process. The command I've used is

Dism.exe /online /Get-FeatureInfo
/FeatureName:Microsoft-Hyper-V

Dism.exe /online /Get-FeatureInfo
/FeatureName:Microsoft-Hyper-V-Management-Clients

This works fine when I execute this from the command line but it fails when I try to execute it through my code. I've tried the 64bit version of Dism.exe under the C:\Windows\SysWoW64 folder but it fails too. Here is the error message I get,

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.

What am I missing here?

UPDATE: SLaks was right, but the issue turned out to be that MS Visual studio express edition by default targets x86 which I had to manually edit in the .csproj file to AnyCPU to make it work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我家小可爱 2024-11-13 02:47:33

由于您在 32 位进程中运行,因此 SysWoW64 中重定向到 32 位版本

运行 %WINDIR%\SysNative\dism.exe 以防止重定向。

Because you're running in a 32-bit process, you're getting redirected to the 32-bit version in SysWoW64

Run %WINDIR%\SysNative\dism.exe to prevent redirection.

如果没有你 2024-11-13 02:47:33

在批处理文件中创建以下内容,例如 RunDism.bat

%WINDIR%\SysNative\dism.exe

在程序中调用批处理文件。 SysNative不是一个真正的文件夹,所以你不能在程序中直接调用上面的代码,它必须由系统调用。这种方法对我有用。

Create below content in a batch file, for example RunDism.bat

%WINDIR%\SysNative\dism.exe

Call the batch file in your program. SysNative is not a real folder, so you cannot call above code in your program directly, it must be call by system. This way is worked for me.

∞梦里开花 2024-11-13 02:47:33

问题是您需要根据系统架构调用适当的dism.exe

正如@eric xu所说,您需要解析路径,因为它不是真正的路径。下面是适合我的代码。它主要检测系统架构,根据架构解析路径,然后调用适当的 dism.exe

string system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32", "dism.exe");
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
    // For 32-bit processes on 64-bit systems, %windir%\system32 folder
    // can only be accessed by specifying %windir%\sysnative folder.
    system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative", "dism.exe");
}

来源:文件系统重定向

The thing is you need to call the appropriate dism.exe dependng on the system architecture.

As @eric xu said, you need to resolve the path because it is not a real path. Below is the code that works for me. It basically detects the system architecture, resolves the path depending on the architecture and then calls the appropriate dism.exe.

string system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "system32", "dism.exe");
if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
{
    // For 32-bit processes on 64-bit systems, %windir%\system32 folder
    // can only be accessed by specifying %windir%\sysnative folder.
    system32Directory = Path.Combine(Environment.ExpandEnvironmentVariables("%windir%"), "sysnative", "dism.exe");
}

Source: File System Redirector

披肩女神 2024-11-13 02:47:33

我必须使用“SysNative\dism.exe”
如果我添加 %WINDIR%\ 它将失败,我正在使用在 Server 2012R2 上安装的 VS2017。
天啊!

I had to use "SysNative\dism.exe"
If I added %WINDIR%\ it would fail, I'm using VS2017 installing on Server 2012R2.
Tnhx!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文