如何正确地将控制台输出重定向到字符串?
您好
我使用Windows Form使用命令行。我正在尝试将控制台输出正确地重定向到字符串,所以我可以使用Regex获取文本,但它不是我想要的,它只是输出这样的东西:
Hello
I work with Windows Form with command lines. I'm trying to redirect console output to string correctly so I can grab the text using Regex but it does not what i wanted to, It just output something like this:
Microsoft Windows [Version 10.0.15063]
(c) 2017 Microsoft Corporation. Alle rettigheder forbeholdes.
D:\example\test >> "D:\example\test\log.txt"
D:\example>aapt.exe dump badging <filepath>
D:\example>
请参阅,它输出版权和我的输入。它并没有从aapt.exe输出任何东西。
这是我使用的代码
See, it outputs copyright and my inputs. It doesn't really output something from aapt.exe.
This is the code i used
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
p.StandardInput.WriteLine("aapt.exe dump badging \"" + path + "\"");
p.StandardInput.Close();
string str = p.StandardOutput.ReadToEnd();
我尝试了什么:
我尝试尝试在互联网上找到的其他代码,但它的确与我的相同,输出版权,版本和输入,不输出任何有用的东西。在互联网上没有什么有用的信息。
我总是不得不将控制台输出重定向到文件作为解决方法,如下所示:
What I have tried:
I tried to try other codes i found on the internet but it does the same way as mine, outputs copyright, version and inputs, doesn't output anything useful. There is nothing much useful infomation on the internet.
I always had to redirect console output to file as a workaround, like this:
p.Start();
p.StandardInput.WriteLine("aapt.exe dump badging \"" + path + "\" >> \"" + logs + "\" 2>&1");
p.StandardInput.Close();
因此它输出以下版本,没有版权,Windows版本和我的输入
So it outputs the following without copyright, windows version and my inputs
package: name='org.schabi.newpipe' versionCode='27' versionName='0.9.0' platformBuildVersionName='7.1.1'
sdkVersion:'15'
targetSdkVersion:'25'
uses-permission: name='android.permission.INTERNET'
uses-permission: name='android.permission.WAKE_LOCK'
uses-permission: name='android.permission.ACCESS_NETWORK_STATE'
uses-permission: name='android.permission.WRITE_EXTERNAL_STORAGE'
uses-permission: name='android.permission.SYSTEM_ALERT_WINDOW'
application-label:'NewPipe'
application-label-af:'NewPipe'
...
我希望你能帮助我。
谢谢。
I hope you can help me.
Thank you.
一个问题可能是你没有知道的方式,当aapt.exe完成执行并读取时任何输出发生之前的流。请记住,您正在创建一个与您的应用程序并行运行的进程。
您通过调用shell然后模拟输入来创建这个不必要的复杂。这里有一个代码段,可以为您执行相同操作并为您提供更多控制权,例如:等待你的过程完成:
One problem could be that you have no way of knowing, when the aapt.exe completes execution and read the streams before any output happened. Remember, you are creating a process that is running in parallel with your application.
You are making this unnecessary complex by invoking a shell and then simulating an input. Here a snippet that should do the same for you and gives you more control, e.g. over waiting for your process to complete:
// Execute the program
p.StartInfo.FileName = "aapt.exe";
p.StartInfo.WorkingDirectory = "<path to aapt.exe>";
p.StartInfo.Arguments = "dump badging \"" + path + "\"";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
p.Start();
string str = p.StandardOutput.ReadToEnd();
p.WaitForExit();
// Execute another program
p2.StartInfo.FileName = "adb.exe";
p2.StartInfo.WorkingDirectory = "<path to adb.exe>";
p2.StartInfo.Arguments = "<arguments for adb.exe>";
p2.StartInfo.UseShellExecute = false;
p2.StartInfo.RedirectStandardOutput = true;
p2.StartInfo.RedirectStandardError = true;
p2.StartInfo.RedirectStandardInput = true;
p2.StartInfo.CreateNoWindow = true;
p2.Start();
string str2 = p2.StandardOutput.ReadToEnd();
p2.WaitForExit();
现在输出应该是str。
根据应用程序写入其输出的流,您可能需要阅读 p.StandardError.ReadToEnd()
。 (在第二个示例中,您使用 2>& 1
重定向到同一个日志文件。)
Now the output should be in str.
Depending to which stream the application writes its output, you might need to read p.StandardError.ReadToEnd()
. (In your second sample you are redirecting both to the same log file with "2>&1
".)