当我尝试在 proc.StandardInput.WriteLine("Y") 中写入内容时,我的应用程序将挂起
My application is going to hang when I try to write something in proc.StandardInput.WriteLine("Y")
我尝试从 C# 代码执行命令。但是当我使用 proc.StandardInput.WriteLine("Y") 我的应用程序挂起。
我的代码如下。
字符串命令="My command after execute it shows some output and required user entry";
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string standard_output;
while ((standard_output = proc.StandardOutput.ReadLine()) != null)
{
if (standard_output.Contains("Are you sure (Y/y or N/n ):"))
{
proc.StandardInput.WriteLine("Y");
//do something
break;
}
}
while ((standard_output = proc.StandardOutput.ReadLine()) != null)
{
if (standard_output.Contains("Are you sure (Y/y or N/n ):"))
{
proc.StandardInput.WriteLine("Y");
string myReturn= proc.StandardInput.ReadLine();
break;
}
}
我尝试从 C# 代码执行命令。但是当我使用 proc.StandardInput.WriteLine("Y") 我的应用程序挂起。
我的代码如下。
字符串命令="My command after execute it shows some output and required user entry";
System.Diagnostics.ProcessStartInfo procStartInfo =
new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);
// The following commands are needed to redirect the standard output.
// This means that it will be redirected to the Process.StandardOutput StreamReader.
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardInput = true;
procStartInfo.UseShellExecute = false;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
// Now we create a process, assign its ProcessStartInfo and start it
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();
string standard_output;
while ((standard_output = proc.StandardOutput.ReadLine()) != null)
{
if (standard_output.Contains("Are you sure (Y/y or N/n ):"))
{
proc.StandardInput.WriteLine("Y");
//do something
break;
}
}
while ((standard_output = proc.StandardOutput.ReadLine()) != null)
{
if (standard_output.Contains("Are you sure (Y/y or N/n ):"))
{
proc.StandardInput.WriteLine("Y");
string myReturn= proc.StandardInput.ReadLine();
break;
}
}