从process.StandardOutput.ReadToEnd()读取进程启动时返回null

AIM:从VSTO Excel Addin Project启动一个新的进程,使用putty(plink)ssh到unix机器上,在那里运行一些脚本并读取StandardOutput以获得结果。


问题: process.StandardOutput.ReadToEnd(); 每次都返回空string。 但是当相同的function是从控制台应用程序调用它完美的作品。 我认为可能有一些身份validation问题,因为在以前的情况下,Excel正在启动该过程,并在稍后用户启动。


我的代码:

  public partial class ThisAddIn { const String DOMAINS_COMMAND ="some_command"; string HOST = "some_host"; private void ThisAddIn_Startup(object sender, System.EventArgs e) { execute(); } private void ThisAddIn_Shutdown(object sender, System.EventArgs e) { } public void execute() { try { Process p = new Process(); ProcessStartInfo ps = new ProcessStartInfo(); ps.FileName = "plink"; ps.Arguments = "-ssh " + HOST + " " + DOMAINS_COMMAND; ps.RedirectStandardOutput = true; ps.RedirectStandardError = true; ps.UseShellExecute = false; ps.CreateNoWindow = true; p.StartInfo = ps; p.Start(); p.WaitForExit(); string rs = p.StandardOutput.ReadToEnd(); MessageBox.Show("value of result:"+rs); } catch { } } #region VSTO generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InternalStartup() { this.Startup += new System.EventHandler(ThisAddIn_Startup); this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown); } #endregion }