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

时间:2015-09-11 11:00:14

标签: c# excel process vsto

AIM:从VSTO Excel Addin项目启动一个新流程,ssh使用putty(plink)在一台unix机器上运行一些脚本并阅读StandardOutput得到结果。

问题:process.StandardOutput.ReadToEnd();每次都返回空字符串。但是当从控制台应用程序调用相同的功能时,它可以完美地工作我认为可能存在一些身份验证问题,因为在之前的情况下,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
    }

0 个答案:

没有答案