【发布时间】:2017-11-19 15:51:32
【问题描述】:
我正在编写一个小型 C# 应用程序来收集在连接到计算机的 Android 设备上运行的应用程序的 logcat 文件。
我可以轻松启动 logcat 并让它将所需的行记录到特定的文本文件中。但是我试图阻止 logcat 记录的每个命令都不起作用。
在使用管理员权限运行我的应用程序时,我也尝试了我的解决方案。
这是我的代码:
static void Main(string[] args)
{
string choice;
string clearLogging = @"adb logcat -c";
string startLogging = @"adb logcat MyApp_LoggingTag:V AndroidRuntime:E *:S > C:\logcat.txt";
string adbDir = @"C:\Users\me\AppData\Local\Android\android-sdk\platform-tools\";
string clearCommand = adbDir + clearLogging;
string startLoggingCommand = adbDir + startLogging;
ProcessStartInfo startInfo = new ProcessStartInfo("cmd.exe", "/K " + clearCommand);
startInfo.CreateNoWindow = true;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
//Tried giving the cmd process elevated rights and then use logcat -c - didn't work
//startInfo.Verb = "runas";
Process logcatRunner = Process.Start(startInfo);
//This works!
logcatRunner.StandardInput.WriteLine(startLoggingCommand);
Console.WriteLine("Logging has started.");
Console.Write("Press Enter to stop logging....");
Console.ReadLine();
//-c doesn't work
//logcatRunner.StandardInput.WriteLine(clearCommand);
//Tried killing adb via the logcatRunner process - doesn't work.
//logcatRunner.StandardInput.WriteLine(@"taskkill -f /im ""adb.exe""");
//Tried killing my process - doesn't work - adb is still running and logcat is still writing logs
//logcatRunner.Kill();
Console.WriteLine("Logging has stopped.");
Console.Write(@"Enter any key");
choice = Console.ReadLine();
}
关闭上述应用程序后,adb 仍在运行。
所以我的问题是,成功启动 adb 和 logcat 后,如何以编程方式关闭它们?
【问题讨论】:
-
如果剩余的
adb.exe进程让您如此困扰 - 最后运行adb kill-server