Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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>


See, it outputs copyright and my inputs. It doesn't really output something from aapt.exe.

This is the code i used

C#
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:

C#
p.Start();
p.StandardInput.WriteLine("aapt.exe dump badging \"" + path + "\" >> \"" + logs + "\" 2>&1");
p.StandardInput.Close();


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.
Posted
Updated 8-Oct-17 7:57am
v2
Comments
Richard MacCutchan 8-Oct-17 3:53am    
Your StartInfo.FileName is cmd.exe, but it should be the full path of aapt.exe.

1 solution

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();

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".)
 
Share this answer
 
v2
Comments
mynametaken 10-Oct-17 9:12am    
Thanks but how about multiple Arguments?
Steve44 10-Oct-17 16:00pm    
Like you would provide multiple arguments in the shell: separated by spaces. The sample already uses 3 arguments:
"dump", "badging" and the path in quotes.
mynametaken 11-Oct-17 4:19am    
Sorry I mean multiple commands like you will execute aapt.exe in other way after the first command and execute cmd.exe, adb.exe etc... after aapt.exe
Steve44 11-Oct-17 8:44am    
Just updated the solution for a second program.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900