[英]Process.Start for system32 applications - The System Cannot Find the File Specified
[英]“File not found” error launching system32\winsat.exe using Process.Start()
我正在尝试使用以下代码运行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();
如果我只调用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:
通过将"winsat.exe"
分配给ProcessStartInfo.FileName
直接调用它。 它因Win32Exception
The system cannot find the file specified
失败: The system cannot find the file specified
如上所述,使用完整路径 - @"c:\\windows\\system32\\winsat.exe"
。 它失败并出现同样的错误。
以系统管理员身份运行代码。 它仍然失败。
如编码示例中那样调用winsat.exe。 它像我之前解释的那样失败了。
有趣的是,从代码启动的命令提示符只能在c:\\ windows \\ system32中看到.dll文件。
有谁知道为什么winsat.exe无法通过System.Diagnostics.Process
启动? 有没有我误解的限制?
谢谢,
雷克斯
使用Windows-on Windows 64位重定向重定向 winsat.exe
。 发生的事情是您的启动请求(来自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();
或者,如果您将程序构建为x64,则可以将路径保留为c:\\windows\\system32
。
请注意,最好使用Environment.GetFolderPath
来获取Windows目录的路径,以防操作系统安装在非标准位置:
WinSSInfo.FileName = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.Windows),
@"sysnative\winsat.exe");
基于SimonMᶜKenzie的回答 ,以及他提供的链接(感谢soyuz的评论)我编写的方法应该适用于任何一种情况(只需复制/粘贴代码):
public static string GetSystem32DirectoryPath()
{
string winDir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
string system32Directory = Path.Combine(winDir, "system32");
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(winDir, "sysnative");
}
return system32Directory;
}
以及启动该过程的代码:
var pi = new ProcessStartInfo
{
FileName = Path.Combine(GetSystem32DirectoryPath(), "winsat.exe"),
CreateNoWindow = true,
UseShellExecute = false
};
Process.Start(pi);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.