C#中Process.Start无法找到ssh-keygen.exe的问题求助
问题原因及解决办法
核心原因:WOW64文件系统重定向
- 你的VS2019默认将项目编译为32位(x86)程序,64位Windows系统中,32位程序访问
C:\Windows\System32时会被自动重定向到C:\Windows\SysWOW64目录,但OpenSSH的ssh-keygen.exe默认安装在64位的System32\OpenSSH下,因此32位程序找不到目标文件。 - VS2022默认项目目标平台为64位(x64),不会触发重定向,所以能正常运行;直接在CMD/PowerShell执行时,64位系统默认终端是64位的,可直接访问
System32下的文件,因此也能正常运行。
解决办法
1. 修改项目目标平台为x64
在VS2019中操作:
- 右键项目 → 属性 → 生成 → 平台目标,选择
x64 - 重新编译并运行程序
2. 临时禁用文件系统重定向(保留32位程序时使用)
通过Windows API临时关闭重定向,访问真实的System32目录,代码示例:
using System; using System.Diagnostics; using System.Runtime.InteropServices; class Program { [DllImport("kernel32.dll", SetLastError = true)] static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr); [DllImport("kernel32.dll", SetLastError = true)] static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr); static void Main(string[] args) { IntPtr wow64Ptr = IntPtr.Zero; try { Wow64DisableWow64FsRedirection(ref wow64Ptr); string command = @"C:\Windows\System32\OpenSSH\ssh-keygen.exe"; Process.Start(command, ""); } finally { if (wow64Ptr != IntPtr.Zero) { Wow64RevertWow64FsRedirection(wow64Ptr); } } } }
3. 使用SysNative虚拟路径(保留32位程序时使用)
将路径替换为@"C:\Windows\SysNative\OpenSSH\ssh-keygen.exe",SysNative是系统提供的虚拟路径,32位程序访问它会直接指向64位的System32目录,无需修改编译平台。
内容的提问来源于stack exchange,提问作者br0therDave
相关产品推荐
相关产品推荐

