使用Process.Start()启动system32\winsat.exe时出现"找不到文件"错误

Rex*_*Hsu 6 c# system.diagnostics process wow64

我正在尝试使用以下代码运行Windows系统评估工具(winsat.exe):

System.Diagnostics.Process WinSPro =
    new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo WinSSInfo = 
    new System.Diagnostics.ProcessStartInfo();
WinSSInfo.FileName = "cmd.exe";
WinSSInfo.Arguments = "/k winsat.exe";
WinSPro.StartInfo = WinSSInfo;
WinSPro.Start();
Run Code Online (Sandbox Code Playgroud)

如果我只调用cmd.exe,这个代码有效,即使我调用regedit.exe它仍然有效.但是,当我尝试将winsat.exe作为cmd.exe的参数调用时,它会失败.命令提示符显示:

'winsat.exe' is not recognized as an internal or external command, 
operable program or batch file.

我尝试了几种方法来调用winsat.exe:

  1. 直接通过指定调用它"winsat.exe"ProcessStartInfo.FileName.它失败了Win32Exception:The system cannot find the file specified

  2. 如上所述,使用完整路径 - @"c:\windows\system32\winsat.exe".它失败并出现同样的错误.

  3. 以系统管理员身份运行代码.它仍然失败.

  4. 如编码示例中那样调用winsat.exe.它像我之前解释的那样失败了.

有趣的是,从代码启动的命令提示符只能在c:\ windows\system32中看到.dll文件.

有谁知道为什么winsat.exe无法启动System.Diagnostics.Process?有没有我误解的限制?

谢谢,

雷克斯

Sim*_*zie 10

winsat.exe使用Windows-on Windows 64位重定向重定向.发生的事情是您的启动请求(来自32位进程)被重定向到%windir%\SysWOW64\winsat.exe.由于在64位安装上没有此特定可执行文件的32位版本,因此启动失败.要绕过此过程并允许32位进程访问本机(64位)路径,您可以%windir%\sysnative改为引用:

Process WinSPro = new Process();
ProcessStartInfo WinSSInfo = new ProcessStartInfo();
WinSSInfo.FileName = @"c:\windows\sysnative\winsat.exe";
WinSPro.StartInfo = WinSSInfo;
WinSPro.Start();
Run Code Online (Sandbox Code Playgroud)

或者,如果您将程序构建为x64,则可以将路径保留为c:\windows\system32.

请注意,最好使用Environment.GetFolderPath获取Windows目录的路径,以防操作系统安装在非标准位置:

WinSSInfo.FileName = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.Windows),
    @"sysnative\winsat.exe");
Run Code Online (Sandbox Code Playgroud)