Search code examples
c#visual-studioprocesscommand-line-argumentsaapt

aapt command works in cmd prompt but not when using Process() in C#


Hey all I am trying to get this command to work using C#'s Process(();

aapt d badging "c:\adb\apks\ftp\ftp.apk" | "C:\adb\grep" package | "c:\adb\sed" -r "s/package: name='([a-z0-9.]*)'.*/\1/"

When I run that in my cmd window it works just fine:

enter image description here

But when I run the same command using the Process() I get the full data:

enter image description here

The output for the passed Arguments variable looks like this:

enter image description here

My Process code is this:

private string Execute(string Arguments, string msg) {
    string text = null;
    Process p = new Process();

    cmdOutputs.AppendText("Sending: " + Arguments + Environment.NewLine);

    p.StartInfo.FileName = adbPath + "aapt.exe";
    p.StartInfo.Arguments = Arguments.Replace("aapt ","");

    if (!deviceID.Equals("")) {
        //Its the first time we are using the app
        //so we need to get the device ID with
        //using the -s command
        p.StartInfo.Arguments = "-s " + deviceID + " " + Arguments;
    }

    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    p.EnableRaisingEvents = true;
    p.Start();

    do {
        Application.DoEvents();

        if (Arguments.Contains("keyevent")) { break; }    
        
        string output = p.StandardOutput.ReadToEnd();
        text += output;

        string err = p.StandardError.ReadToEnd();
        text += err;
    } while (!p.HasExited);

    text += msg;

    cmdOutputs.AppendText("Completed: " + Environment.NewLine);

    cmdOutputs.SelectionColor = Color.Tan;
    cmdOutputs.SelectionFont = new Font(cmdOutputs.Font, FontStyle.Bold);
    cmdOutputs.AppendText(text.Replace("\r\r\n", Environment.NewLine) + Environment.NewLine);
    cmdOutputs.SelectionColor = Color.White;
    cmdOutputs.SelectionFont = new Font(cmdOutputs.Font, FontStyle.Regular);    
    cmdOutputs.SelectionStart = cmdOutputs.TextLength;
    cmdOutputs.ScrollToCaret();

    return text;
}

Can anyone spot where I am missing something that's needed?


Solution

  • Thanks to @RetiredNinja ths is what needed to be done:

    private string Execute(string Arguments, string msg) {
        string text = null;
        Process p = new Process();
    
        cmdOutputs.AppendText("Sending: " + Arguments + Environment.NewLine);
    
        //p.StartInfo.FileName = adbPath + "aapt.exe";
        //p.StartInfo.Arguments = Arguments.Replace("aapt ","");
        p.StartInfo.FileName = "cmd.exe";
        p.StartInfo.Arguments = "/C " + adbPath + "aapt.exe" + Arguments.Replace("aapt ","");
    
        if (!deviceID.Equals("")) {
            //Its the first time we are using the app
            //so we need to get the device ID with
            //using the -s command
            p.StartInfo.Arguments = "-s " + deviceID + " " + Arguments;
        }
    
        p.StartInfo.RedirectStandardError = true;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.CreateNoWindow = true;
        p.EnableRaisingEvents = true;
        p.Start();
    
        do {
            Application.DoEvents();
    
            if (Arguments.Contains("keyevent")) { break; }    
            
            string output = p.StandardOutput.ReadToEnd();
            text += output;
    
            string err = p.StandardError.ReadToEnd();
            text += err;
        } while (!p.HasExited);
    
        text += msg;
    
        cmdOutputs.AppendText("Completed: " + Environment.NewLine);
    
        cmdOutputs.SelectionColor = Color.Tan;
        cmdOutputs.SelectionFont = new Font(cmdOutputs.Font, FontStyle.Bold);
        cmdOutputs.AppendText(text.Replace("\r\r\n", Environment.NewLine) + Environment.NewLine);
        cmdOutputs.SelectionColor = Color.White;
        cmdOutputs.SelectionFont = new Font(cmdOutputs.Font, FontStyle.Regular);    
        cmdOutputs.SelectionStart = cmdOutputs.TextLength;
        cmdOutputs.ScrollToCaret();
    
        return text;
    }
    

    The addition was:

    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.Arguments = "/C " + Arguments.Replace("aapt ","");