How to execute multiple batch commands inside a C# forms project

0

So i want to run a bunch of different batch commands that do different things. Currently my software works by running the .bat file that is included with the software, but i want to simply remove the syntax from the .bat file and integrate it inside my software so when i click the button it runs the code directly and not launch the .bat file.

Problem is how do i run multiple batch commands, not only that but also wait for each command to finish running just like batch does? Overall i wanna execute batch commands like stop services, delete registry files, delete folders and software. PS : this is a C# Forms application ( so i click one button and it all executes )

Currently i have : with examples

var proc1 = new ProcessStartInfo();
string stopservices = 

    "net stop \"serviceexample1\"" +
    "net stop \"serviceexample2\""; 

string deleteregistry = 
"REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\EXAMPLE\\.. / t REG_DWORD / v Start / d 0x00000004 / f" +

"rem REG ADD HKEY_LOCAL_MACHINE\\SYSTEM\\EXAMPLE\\.. /t REG_SZ /v Start /d 5 /f";

string deletesoftware = 
"MsiExec.exe /X{EXAMPLESOFTWARE} /quiet" + 
"MsiExec.exe /X{EXAMPLESOFTWARE} /quiet";

string removedirectories = 
"rmdir /s /q C:\\Program Files\\EXAMPLE" +
"rmdir /s /q C:\\Program Files(x86)\\EXAMPLE";

proc1.UseShellExecute = true;

proc1.WorkingDirectory = @"C:\Windows\System32";

proc1.FileName = @"C:\Windows\System32\cmd.exe";
proc1.Verb = "runas";

proc1.Arguments = "/c " + stopservices + deleteregistry + deletesoftware + removedirectories;


proc1.WindowStyle = ProcessWindowStyle.Normal;
System.Diagnostics.Process processwait = System.Diagnostics.Process.Start(proc1);

while (!processwait.HasExited && processwait.Responding)
{
    System.Threading.Thread.Sleep(100);
}

I have tried to look in internet how to run batch code directly from c# and the only way i found how to do it was using a string. But i dont think that will wait for each command to finish to begin the next one because a string is just a line of text so it will take it all at once.

I also wanna make sure that when the code was successfully executed the command prompt will stay untill i close it ( like pause in batch ).

Sorry if this is a simple question, i am a student with less then half a year experience in c#

c#
windows
batch-file
asked on Stack Overflow Nov 9, 2018 by John Linaer • edited Nov 9, 2018 by Rob

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0