aapt 命令在 cmd 提示符下有效,但在 C# 中使用 Process() 时无效

问题描述 投票:0回答:1

嘿,我正在尝试使用 C# 的 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/"

当我在 cmd 窗口中运行它时,它工作得很好:

但是当我使用 Process() 运行相同的命令时,我得到了完整的数据:

传递的 Arguments 变量的输出如下所示:

我的流程代码是这样的:

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;
}

有人能发现我在哪里缺少了需要的东西吗?

c# visual-studio process command-line-arguments aapt
1个回答
0
投票

感谢@RetiredNinja,这就是需要做的事情:

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;
}

补充的是:

p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/C " + Arguments.Replace("aapt ","");
© www.soinside.com 2019 - 2024. All rights reserved.