C# 阅读控制台 StandardOutput
C# reading console StandardOutput
我正在尝试 运行 从 C# winform 应用程序控制台命令并读取它们的输出,但是当我这样做时,输出包含 Windows "copyright".
我不认为我可以在每个 \n
字符上拆分输出并删除起始行,因为并非所有 cmd 版本似乎都输出垃圾数据,所以有没有办法仅获取命令的 "response" 而不是作为输出出现在控制台中的整个文本?
这是我的代码:
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
cmd.StandardInput.WriteLine("home-server scan");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
String output = cmd.StandardOutput.ReadToEnd();
String[] devices = output.Split('\n');
textBox1.Text = output;
这是我的输出(不要介意控制台颜色代码):
Microsoft Windows [Version 10.0.16299.309]
(c) 2017 Microsoft Corporation. All rights reserved.
C:\users\corrado\Documents\Programming\Projects\home-server-gui\home-server-gui\home-server-gui\bin\Debug>home-server scan
[32mFound following devices:[39m
[32mvodafone.station[39m: http://192.168.1.1:8080
C:\users\corrado\Documents\Programming\Projects\home-server-gui\home-server-gui\home-server-gui\bin\Debug>
如何删除所有垃圾文本?
发生这种情况是因为您 运行 cmd.exe 来自 Process。
只需将其更改为直接调用您的 home-server.exe 即可。
这应该有效。
我正在尝试 运行 从 C# winform 应用程序控制台命令并读取它们的输出,但是当我这样做时,输出包含 Windows "copyright".
我不认为我可以在每个 \n
字符上拆分输出并删除起始行,因为并非所有 cmd 版本似乎都输出垃圾数据,所以有没有办法仅获取命令的 "response" 而不是作为输出出现在控制台中的整个文本?
这是我的代码:
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.CreateNoWindow = true;
cmd.Start();
cmd.StandardInput.WriteLine("home-server scan");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
String output = cmd.StandardOutput.ReadToEnd();
String[] devices = output.Split('\n');
textBox1.Text = output;
这是我的输出(不要介意控制台颜色代码):
Microsoft Windows [Version 10.0.16299.309]
(c) 2017 Microsoft Corporation. All rights reserved.
C:\users\corrado\Documents\Programming\Projects\home-server-gui\home-server-gui\home-server-gui\bin\Debug>home-server scan
[32mFound following devices:[39m
[32mvodafone.station[39m: http://192.168.1.1:8080
C:\users\corrado\Documents\Programming\Projects\home-server-gui\home-server-gui\home-server-gui\bin\Debug>
如何删除所有垃圾文本?
发生这种情况是因为您 运行 cmd.exe 来自 Process。 只需将其更改为直接调用您的 home-server.exe 即可。 这应该有效。