C# 从process.StandardOutput.ReadToEnd()读取时,进程启动时返回null
目标:从C# 从process.StandardOutput.ReadToEnd()读取时,进程启动时返回null,c#,excel,process,vsto,C#,Excel,Process,Vsto,目标:从VSTOExcel加载项项目启动一个新流程,该项目使用putty(plink)运行一些脚本,并阅读StandardOutput以获得结果 问题:process.StandardOutput.ReadToEnd()每次都返回空字符串。但是,当从控制台应用程序调用相同的函数时,它可以完美地工作。我认为可能存在一些身份验证问题,因为在前一种情况下Excel正在启动该过程,而在后一种情况下用户启动 我的代码: public partial class ThisAddIn {
VSTO
Excel加载项项目启动一个新流程,该项目使用putty(plink)
运行一些脚本,并阅读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
}
public分部类ThisAddIn
{
const String DOMAINS\u COMMAND=“some\u COMMAND”;
string HOST=“some_HOST”;
私有void ThisAddIn_启动(对象发送方,System.EventArgs e)
{
执行();
}
私有void ThisAddIn_关闭(对象发送方,System.EventArgs e)
{
}
public void execute()
{
尝试
{
过程p=新过程();
ProcessStartInfo ps=新的ProcessStartInfo();
ps.FileName=“plink”;
ps.Arguments=“-ssh”+主机+”“+域\u命令;
ps.RedirectStandardOutput=true;
ps.error=true;
ps.UseShellExecute=false;
ps.CreateNoWindow=true;
p、 StartInfo=ps;
p、 Start();
p、 WaitForExit();
字符串rs=p.StandardOutput.ReadToEnd();
MessageBox.Show(“结果值:+rs”);
}
抓住
{ }
}
#区域VSTO生成的代码
///
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
///
私有void InternalStartup()
{
this.Startup+=new System.EventHandler(ThisAddIn\u启动);
this.Shutdown+=new System.EventHandler(ThisAddIn\u Shutdown);
}
#端区
}