Categories
Mastering Development

How to Run a .cmd file from C# which has multiple lines of code inside it

When I try to execute the cmd, the code inside the cmd is not getting executed. I expect that when I execute the cmd it should run the code inside that and does the operation. When I do by double-clicking the cmd file whole processes finish nicely. but through c#, the code inside the cmd file doesn’t execute.

static void Main(string[] args)
{
    var proc = new Process();
    proc.StartInfo.FileName = @"D:\Dump\createAndDump.cmd";
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.Start();
    string outPut = proc.StandardOutput.ReadToEnd();
           
    proc.WaitForExit();
    var exitCode = proc.ExitCode;
    proc.Close();
}

the cmd has following code to execute:
this connects to the tool and flashing of device is done.
I am doing the code as stated above but it is not able to execute the lines inside the cmd file.

@echo connect> flash.mtb


@for %%i in (*.s11) do (

@echo open_file "%%~fi">> flash.mtb
@echo select_all_sections>> flash.mtb
@echo add_selected_sections>>flash.mtb
)

@echo program>> flash.mtb
@echo disconnect>> flash.mtb
@echo exit>> flash.mtb

for %%j in (flash.mtb) do "C:\Program Files (x86)\xyz\Memtool 4.7\IMTtool.exe" %%~dpj%%~nxj

Leave a Reply

Your email address will not be published. Required fields are marked *