3

This is my first time creating a batch. basically i want write commands in the command Promt. so i need batch file so that i can it in c# and does the task.

The commands looks like this:

install PortName=COM50-
bcdedit.exe -set TESTSIGNING OFF

How i can create the batch file and run it using c# code.

Thanks

Details: i am using com0com to create virtual ports, so the main idea is to automate the process, so i can create port without going to command port and write the commands.

Liban
  • 571
  • 5
  • 17
  • 32
  • 1
    Open notepad, save the command in file with extension `.bat`, then [use that batch file in C# Code](http://stackoverflow.com/questions/394036/how-to-execute-a-bat-file-from-a-c-sharp-windows-form-app) – Habib Dec 05 '12 at 08:57
  • See here for how to run a batch file from C#: http://stackoverflow.com/questions/5519328/executing-batch-file-in-c-sharp – Matthew Watson Dec 05 '12 at 08:58

3 Answers3

5

Write the commands to a file and call System.Diagnostics.Process.Start() with the path to the file.

Zach Johnson
  • 21,761
  • 6
  • 66
  • 83
5

You maybe don't need a batch file :-

 Process myprocess = new Process();
 myprocess.StartInfo.FileName = @"C:\WHERE_EVER\bcdedit.exe";
 // I dont know the exact switch, but im sure you would be able to work this out.
 myprocess.StartInfo.Arguments = @"Install PortName=COM50 -set TESTSIGNING OFF";
 myprocess.Start();
Derek
  • 7,530
  • 9
  • 49
  • 80
  • ` myprocess.StartInfo.Arguments = @"Install PortName=COM50 -set TESTSIGNING OFF"; ` doest work. when the command prompt starts, how do u execute inside it. basically how to pass the command? – Liban Dec 06 '12 at 05:28
  • That's something you would need to check out yourself, I just gave that switch as an example. I dont know what BCDedit is. – Derek Dec 06 '12 at 08:19
  • i fixed it.. thanks.. i wrote the command in batch file and run it, it works fine. – Liban Dec 06 '12 at 09:04
2

System.Diagnostics.Process is your best option.

Provides access to local and remote processes and enables you to start and stop local system processes.

For example;

System.Diagnostics.Process.Start("c:\\yourfilename.bat");
Soner Gönül
  • 91,172
  • 101
  • 184
  • 324