从 System.Diagnostics.Process 调用 dism.exe 失败
为了在 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-VDism.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您在 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.在批处理文件中创建以下内容,例如 RunDism.bat
在程序中调用批处理文件。 SysNative不是一个真正的文件夹,所以你不能在程序中直接调用上面的代码,它必须由系统调用。这种方法对我有用。
Create below content in a batch file, for example RunDism.bat
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.
问题是您需要根据系统架构调用适当的
dism.exe
。正如@eric xu所说,您需要解析路径,因为它不是真正的路径。下面是适合我的代码。它主要检测系统架构,根据架构解析路径,然后调用适当的
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
.Source: File System Redirector
我必须使用“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!