2011-07-12 73 views
1

可以说,我有一个.bat文件,其中文字样 回声HELLO 暂停通过Cmd.exe C#获取字符串

输出“HELLO”。 没有别的

如果我从C#开始进程,也许我想在进程中闲置,并把它输出到一个字符串的一切? 就像我扫描过程。

我得到的代码是这样做的,但是像0秒之后,当进程启动时,它需要输出什么,但只有第一个。 如果我有一个100行的bat文件,那么它只需要1个也许。

总而言之,一旦bat文件输出新的东西,我想在我的C#应用​​程序中得到它。

请帮

示例代码:

Directory.SetCurrentDirectory(@"C:\Users\WS\Desktop\Modded\"); 
Process p = new Process(); 
p.StartInfo.FileName = @"C:\Users\WS\Desktop\Modded\Launcher.bat"; 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.CreateNoWindow = true; 
p.StartInfo.RedirectStandardOutput = true; 
p.Start(); 
txtLog.Text = 
p.StandardOutput.ReadToEnd(); 
p.Close(); 

~~ redpois0n

+3

向我们显示您当前的代码。 (如何准备示例代码:http://tinyurl.com/so-hints) – Heinzi

+0

Ill add now .... Directory.SetCurrentDirectory(@“C:\ Users \ WS \ Desktop \ Modded \”); 进程p = new Process(); p.StartInfo.FileName = @“C:\ Users \ WS \ Desktop \ Modded \ Launcher.bat”; p.StartInfo.UseShellExecute = false; p.StartInfo.CreateNoWindow = true; p.StartInfo.RedirectStandardOutput = true; p.Start(); txtLog.Text = p.StandardOutput.ReadToEnd(); p.Close(); – redpois0n

+0

@ user841016:不要把它放在评论里,它隐藏在那里,不可读 - 放在*问题*中。 –

回答

0

如果我理解你的要求正确,它应该返回一个字符串数组,它听起来像你只检索/显示数组的第一个元素。如果命令行有很多参数,则需要循环遍历数组,直到数组的大小。

此外,txtLog可能需要更改为: txtLog.Text + = p.StandardOutput.ReadToEnd(); 如果您追加到日志。

+0

是的,但它是一个Minecraft服务器,可以随时“生成”新的字符串。像BLLALBLALB加入了游戏。 如果我不把p.Close();它不会返回任何东西。 和p.StandardOutPut.ReadToend()中的循环也不会工作,我想要一个实时流 – redpois0n

+0

你可以循环p.StandardOutPut.ReadLine();而不是“readToend();” –

+0

是的,但我必须调用p.Stop();得到一些东西 – redpois0n