Here be my stupid ? of the day. The following code will not run in order, (as in top to bottom), it starts top skips to bottom then runs middle. IT will on the other hand run top down if i put a Console.WriteLine(); and Console.ReadKey(true); in the middle but i would like it to run straight through without interruption. I hope this makes since. I’m not sure if this has anything to do with it, but I’m trying to use a shell program (adb.exe) with this and not sure if that is throwing things off or not. thank you for any help you might be able to give.
also, i know there are shorter ways of writing some of this, some of which i know and some i don’t. feel free to enlighten me if you can. ( i have searched many places to find the answer to this such as msdn to no avail).
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);
}
}
}
The program does run in order.
I suspect your confusion is that you’re launching other programs. By default, the
Processclass doesn’t wait formyProcessto exit before you startmyProcess1. To get this to wait, add a call toProcess.WaitForExit():