我有一个控制台应用程序和一个win表单应用程序,它们都需要调用远程服务器获取某些数据,他们调用Putty的命令行部分plink.exe来通过SSH运行远程命令.
我创建了一个小型类库,供两者共享,运行以下内容:
public static string RunCommand(string command, string arguments) {
ProcessStartInfo startInfo = new ProcessStartInfo {
FileName = command,
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true
};
string output = null;
using (Process p = new Process()) {
p.StartInfo = processStartInfo;
p.Start();
output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
return output;
}
Run Code Online (Sandbox Code Playgroud)
在控制台应用程序下一切正常,在win表单下它没有错误,似乎WaitForExit()只是不等待.我得到一个空字符串输出.我已经从远程服务器确认用户已登录,因此命令似乎已运行.
有任何想法吗?