嘿,我正在尝试使用 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;
}
有人能发现我在哪里缺少了需要的东西吗?
感谢@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 ","");