“File not found” error launching system32\winsat.exe using Process.Start()

前端 未结 2 706
别那么骄傲
别那么骄傲 2020-12-20 21:21

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

System.Diagnostics.Process WinSPro =
    new System.Diagnostics.         


        
2条回答
  •  春和景丽
    2020-12-20 22:00

    Based on Simon MᶜKenzie's answer, and the link he provided (thanks to soyuz for his comment) I wrote method that should work in either cases (to just copy/paste the code):

    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;
    }
    

    and code to launch the process:

    var pi = new ProcessStartInfo
    {
        FileName = Path.Combine(GetSystem32DirectoryPath(), "winsat.exe"),
        CreateNoWindow = true,
        UseShellExecute = false
    };
    Process.Start(pi);
    

提交回复
热议问题