Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
732 views
in Technique[技术] by (71.8m points)

c# - "File not found" error launching system32winsat.exe using Process.Start()

I'm trying to run the Windows System Assessment Tool (winsat.exe) using the following code:

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();

This code works if I only call cmd.exe, and even if I call regedit.exe it still works. However, when I try to call winsat.exe as a argument of cmd.exe, it fails. The command prompt shows this:

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

I tried several ways to call winsat.exe:

  1. Call it directly by assigning "winsat.exe" to ProcessStartInfo.FileName. It fails with a Win32Exception: The system cannot find the file specified

  2. As above, using the full path - @"c:windowssystem32winsat.exe". It fails with the same error.

  3. Run the code as the System Administrator. It still fails.

  4. Call winsat.exe as in the coded example. It failed as I explained earlier.

It's interesting that the command prompt launched from the code can only see .dll files in c:windowssystem32.

Does anyone have any idea why winsat.exe cannot be launched through System.Diagnostics.Process? Are there any limitations which I've misunderstood?

Thanks,

Rex

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

winsat.exe is redirected using Windows-on Windows 64-bit redirection. What's happening is that your launch request (from a 32-bit process) is being redirected to %windir%SysWOW64winsat.exe. Since there's no 32-bit version of this particular executable on 64-bit installs, the launch fails. To bypass this process and allow your 32-bit process to access the native (64-bit) path, you can reference %windir%sysnative instead:

Process WinSPro = new Process();
ProcessStartInfo WinSSInfo = new ProcessStartInfo();
WinSSInfo.FileName = @"c:windowssysnativewinsat.exe";
WinSPro.StartInfo = WinSSInfo;
WinSPro.Start();

Alternatively, if you build your program as x64, you can leave the path as c:windowssystem32.

Note that it's best to use Environment.GetFolderPath to get the path to the windows directory, just in case the OS is installed in a non-standard location:

WinSSInfo.FileName = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.Windows),
    @"sysnativewinsat.exe");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...