Search code examples
c#visual-studio-2019

C# Process.Start can't find exe (ssh-keygen)


This is failing with "the system cannot find the file specified."

static void Main(string[] args)
{
   string command = @"C:\Windows\System32\OpenSSH\ssh-keygen.exe";
   Process.Start(command, "");'
}

If I cut and paste the path to the "Developer PowerShell" it does run from there. If I cut and paste the path to an admin or non-admin cmd prompt or powershell window it works. But the above code fails whether I compile and run it outside of vs or via the debugger. If I sub in string string command = @"C:\Windows\System32\notepad.exe" that works. If cut and paste the code to a different VS on a different machine it works. The failing VS is 2019 and the succeeding is 2022 but can't see how that would impact things. Any idea what's going on?


Solution

  • The issue is most likely due to the File System Redirector.

    Try the following:

    string command = string.Empty;
    
    //environment variable windir has the same value as SystemRoot
    //use 'Sysnative' to access 64-bit files (in System32) if program is running as 32-bit process
    //use 'SysWow64' to access 32-bit files on 64-bit OS
    
    
    if (Environment.Is64BitOperatingSystem && !Environment.Is64BitProcess)
    {
        command = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"), "Sysnative", "OpenSSH", "ssh-keygen.exe");
    }
    else
    {
        command = System.IO.Path.Combine(Environment.GetEnvironmentVariable("windir"),"OpenSSH", "ssh-keygen.exe");
    }