在 C# Windows 窗体中重定向 schtasks 输出
我有一个我无法弄清楚的问题。我有一个带有文本框、按钮和列表框的 Windows 窗体。我想在文本框中输入 IP,按下按钮,然后将 schtasks 输出重定向到我的列表框。然而,除了第一行之外,我从来没有得到过任何东西。另外,我的代码在重定向到文本文件时工作正常。下面是我的代码。
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);
我写入文本文件的代码是相同的,除了最后,我没有写入列表框,而是创建了一个文本编写器并为其提供了文件的位置。谁能弄清楚我做错了什么?
I have an issue that I can not figure out. I have a Windows form with a Text Box, Button, and List Box. I want to type an IP into the text box, push the button, and redirect the schtasks output to my list box. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要更多地解析输出以将其添加到列表框中。我使用文本框运行了您的示例并获得了所有输出,就像在命令行中一样。我认为列表框只是将所有输出添加为一项?
尝试这样的事情:
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: