[英]Redirecting schtasks output in C# Windows Form
I have an issue that I can not figure out. 我有一个我无法弄清楚的问题。 I have a Windows form with a Text Box, Button, and List Box. 我有一个带有文本框,按钮和列表框的Windows窗体。 I want to type an IP into the text box, push the button, and redirect the schtasks output to my list box. 我想在文本框中键入IP,按下按钮,然后将schtasks输出重定向到我的列表框。 However, I never get anything more than the first line. 但是,我从来没有得到比第一行更多的东西。 Also, my code works fine when redirecting to a text file. 此外,我的代码在重定向到文本文件时工作正常。 Below is my code. 以下是我的代码。
string machineName = textBox1.Text;
Process process = new Process();
process.StartInfo.FileName = "schtasks";
process.StartInfo.Arguments = " /query /s " + machineName;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
lstOutput.Items.Add(output);
My code to write to text file was the same except at the end, instead of writing to the listbox, I created a text writer and gave it a location for the file. 我写入文本文件的代码是相同的,除了最后,我创建了一个文本编写器,并给它一个文件的位置,而不是写入列表框。 Can anyone figure out what I have done wrong? 任何人都可以弄清楚我做错了什么?
I think you need to parse the output more to massage it into the list box. 我认为您需要更多地解析输出以将其按到列表框中。 I ran your example with a textbox and got all of the output, just like at a command line. 我用一个文本框运行你的例子,得到了所有的输出,就像在命令行一样。 I think the list box is just adding all of the output as one item maybe? 我认为列表框只是将所有输出添加为一个项目?
Try something like this: 尝试这样的事情:
string[] lines = output.Split('\n');
foreach (string s in lines)
{
lbResult.Items.Add(s);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.