.NET (C#) 中的“StandardIn 未重定向”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8826884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"StandardIn has not been redirected" error in .NET (C#)
提问by
I want to do a simple app using stdin. I want to create a list in one program and print it in another. I came up with the below.
我想使用标准输入做一个简单的应用程序。我想在一个程序中创建一个列表并在另一个程序中打印它。我想出了下面的内容。
I have no idea if app2 works however in app1 I get the exception "StandardIn has not been redirected." on writeline (inside the foreach statement). How do I do what I intend?
我不知道 app2 是否有效,但是在 app1 中我收到异常“StandardIn 尚未重定向”。在 writeline 上(在 foreach 语句内)。我如何做我想做的事?
NOTE: I tried setting UseShellExecute to both true and false. Both cause this exception.
注意:我尝试将 UseShellExecute 设置为 true 和 false。两者都会导致此异常。
//app1
{
var p = new Process();
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.FileName = @"path\bin\Debug\print_out_test.exe";
foreach(var v in lsStatic){
p.StandardInput.WriteLine(v);
}
p.StandardInput.Close();
}
//app 2
static void Main(string[] args)
{
var r = new StreamReader(Console.OpenStandardInput());
var sz = r.ReadToEnd();
Console.WriteLine(sz);
}
采纳答案by itsme86
You never Start() the new process.
你永远不会 Start() 新进程。
回答by Chris Shain
You must set UseShellExecute to false if you want to set RedirectStandardInput to true. Otherwise, writing to the StandardInput stream throws an exception.
如果要将 RedirectStandardInput 设置为 true,则必须将 UseShellExecute 设置为 false。否则,写入 StandardInput 流会引发异常。
One might expect it to be false by default, that doesn't seem to be the case.
人们可能认为默认情况下它是假的,但情况似乎并非如此。
回答by competent_tech
You must ensure ShellExecute is set to false in order for the redirection to work correctly.
您必须确保将 ShellExecute 设置为 false 才能使重定向正常工作。
You should also open a streamwriter on it, start the process, wait for the process to exit, and close the process.
您还应该在其上打开一个流写入器,启动进程,等待进程退出,然后关闭进程。
Try replacing these lines:
尝试替换这些行:
foreach(var v in lsStatic){
p.StandardInput.WriteLine(v);
}
p.StandardInput.Close();
with these:
用这些:
p.Start();
using (StreamWriter sr= p.StandardInput)
{
foreach(var v in lsStatic){
sr.WriteLine(v);
}
sr.Close();
}
// Wait for the write to be completed
p.WaitForExit();
p.Close();
回答by MethodMan
if you would like to see a simple example of how to write your process to a Stream use this code below as a Template feel free to change it to fit your needs..
如果您想查看如何将流程写入流的简单示例,请使用下面的代码作为模板,请随意更改它以满足您的需要。
class MyTestProcess
{
static void Main()
{
Process p = new Process();
p.StartInfo.UseShellExecute = false ;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"path\bin\Debug\print_out_test.exe";
p.StartInfo.CreateNoWindow = true;
p.Start();
System.IO.StreamWriter wr = p.StandardInput;
System.IO.StreamReader rr = p.StandardOutput;
wr.Write("BlaBlaBla" + "\n");
Console.WriteLine(rr.ReadToEnd());
wr.Flush();
}
}
//Change to add your work with your for loop
//更改为使用 for 循环添加您的工作

