这是我的笨蛋吗?的一天。以下代码不会按顺序运行(如从上到下),它从顶部跳到底部然后运行中间。另一方面,如果我放一个 Console.WriteLine(); 它将自上而下运行。和 Console.ReadKey(true);在中间,但我希望它直接运行而不会中断。我希望这是从那时起。我不确定这是否与它有关,但我正在尝试使用 shell 程序(adb.exe),但不确定这是否会导致问题。感谢您提供的任何帮助。
另外,我知道有一些更短的方法来写这些,有些我知道,有些我不知道。如果可以的话,请随时启发我。 (我已经搜索了很多地方以找到答案,例如 msdn 无济于事)。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
public void Main(string[] args)
{
// set consolsize to fit text output
Console.SetWindowSize(150, 50);
// creat test text file
var cd = Directory.GetCurrentDirectory();
string filetest = Path.Combine(cd + "\\android\\5_tools\\test.txt");
string brand = (cd + "\\android\\5_tools\\brand.txt");
string model = (cd + "\\android\\5_tools\\model.txt");
string sdk = (cd + "\\android\\5_tools\\sdk.txt");
string amodex = (cd + "\\android\\4_framework\\am.odex");
string phoneodex = (cd + "\\android\\3_app\\Phone.odex");
string testapp = (cd + "\\android\\3_app\\test.txt");
string testframe = (cd + "\\android\\4_framework\\test.txt");
Console.WriteLine(cd);
// creating folders to work with
Directory.CreateDirectory(cd + "\\android");
Directory.CreateDirectory(cd + "\\android\\1_Compile");
Directory.CreateDirectory(cd + "\\android\\2_Decompile");
Directory.CreateDirectory(cd + "\\android\\3_App");
Directory.CreateDirectory(cd + "\\android\\4_Framework");
Directory.CreateDirectory(cd + "\\android\\5_tools");
Directory.CreateDirectory(cd + "\\android\\6_Saved_classes");
File.WriteAllText(filetest, "DO NOT DELETE!!!! This is a test file to be sure device mounts in RW mode to be sure all commands work");
// set adb commands
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd";
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.UseShellExecute = false;
myProcess.Start();
// running adb commands, and setting up device for transfere to and from
myProcess.StandardInput.WriteLine("adb kill-server");
myProcess.StandardInput.WriteLine("adb start-server");
myProcess.StandardInput.WriteLine("adb shell");
myProcess.StandardInput.WriteLine("su");
myProcess.StandardInput.WriteLine("mount -o rw,remount /system");
myProcess.StandardInput.WriteLine("chmod 777 /system");
myProcess.StandardInput.WriteLine("chmod 777 /system/framework");
myProcess.StandardInput.WriteLine("chmod 777 /system/app");
//getting build props
myProcess.StandardInput.WriteLine("getprop ro.product.brand > /sdcard/brand.txt");
myProcess.StandardInput.WriteLine("getprop ro.product.model > /sdcard/model.txt");
myProcess.StandardInput.WriteLine("getprop ro.build.version.sdk > /sdcard/sdk.txt");
myProcess.StandardInput.WriteLine("exit");
myProcess.StandardInput.WriteLine("exit");
Process myProcess1 = new Process();
myProcess1.StartInfo.FileName = "cmd";
myProcess1.StartInfo.RedirectStandardInput = true;
myProcess1.StartInfo.UseShellExecute = false;
myProcess1.Start();
// pulling need files from android
myProcess1.StandardInput.WriteLine("adb pull /sdcard/brand.txt " + (brand));
myProcess1.StandardInput.WriteLine("adb pull /sdcard/model.txt " + (model));
myProcess1.StandardInput.WriteLine("adb pull /sdcard/sdk.txt " + (sdk));
myProcess1.StandardInput.WriteLine("adb pull /system/app/Phone.odex " + (phoneodex));
myProcess1.StandardInput.WriteLine("adb pull /system/framework/am.odex " + (amodex));
//testing mount
myProcess1.StandardInput.WriteLine("adb push " + (filetest) + " /system/app/test.txt");
myProcess1.StandardInput.WriteLine("adb push " + (filetest) + " /system/framework/test.txt");
myProcess1.StandardInput.WriteLine("adb pull /system/app/test.txt " + (testapp));
myProcess1.StandardInput.WriteLine("adb pull /system/framework/test.txt " + (testframe));
//Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
Console.WriteLine(File.Exists(testapp) ? "File exists." : "File does not exist.");
Console.WriteLine(File.Exists(testframe) ? "File exists." : "File does not exist.");
//clearing test file
myProcess1.StandardInput.WriteLine("adb shell");
myProcess1.StandardInput.WriteLine("su");
myProcess1.StandardInput.WriteLine("rm /system/app/test.txt");
myProcess1.StandardInput.WriteLine("rm /system/framework/test.txt");
myProcess1.StandardInput.WriteLine("exit");
myProcess1.StandardInput.WriteLine("exit");
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);
}
}
}
程序 按顺序运行。
我怀疑您的困惑是您正在启动其他程序。默认情况下,Process
类不等待 myProcess
开始前退出myProcess1
.要让它等待,请添加对 Process.WaitForExit()
的调用:
myProcess.StandardInput.WriteLine("exit");
// Add a line here!
// This will cause it to wait until the first process completes
myProcess.WaitForExit();
Process myProcess1 = new Process();
myProcess1.StartInfo.FileName = "cmd";
我是一名优秀的程序员,十分优秀!