Calling dism.exe from System.Diagnostics.Process Fails

馋奶兔 提交于 2019-11-29 09:44:41

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.

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!