How to use additional DOS commands in C #

I have about 7 commands in DOS, and I want to run them in my C # program. Can i do:

System.Diagnostics.Process.Start("cmd.exe", "my more commands here"); 

? EDIT: I am making a small application that will work g ++. Is it correct?:

  System.Diagnostics.Process.Start("cmd.exe", "/k cd C:\\Alps\\compiler\\ /k g++ C:\\Alps\\" + project_name + "\\Debug\\Main.cpp"); 

The command to compile:

 g++ -c C:\Alps\here_is_projectname\Debug\Main.cpp -o main.o 
+6
source share
4 answers
 cmd.exe /k <command> cmd.exe /c <command> 

Both are valid.

  • /k will execute the command and leave you with an empty prompt (perhaps less desirable in your application if you just want to do the feedback).
  • /c will execute the command and close the window when it is completed.

If you want to execute a command from a specific directory, you can do:

 System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe"); p.StartInfo.Arguments = String.Format(@"/c g++ ""C:\Alps\{0}\Debug\Main.cpp""", project_name); p.StartInfo.WorkingDirectory = @"C:\Alps\compiler"; p.StartInfo.CreateNoWindow = true; p.StartInfo.ErrorDialog = false; p.Start(); 
+7
source

Yes, you can go to the command line with the "/ C" switch:

 System.Diagnostics.Process.Start("cmd.exe", "/C dir"); 
+7
source

You can also do the following:

 Process.Start(new ProcessStartInfo() { Arguments = "args", WorkingDirectory = "C:\SomePath", UseShellExecute= true, FileName = ".exe" }); 

There are also options on processstartinfo to redirect input and output if you need to

For instance..

 Process.Start(new ProcessStartInfo() { Arguments = "C:\\Alps\\" + project_name + "\\Debug\\Main.cpp", WorkingDirectory = "C:\\Apls\\", UseShellExecute= true, FileName = "g++.exe" }); 
+3
source

You can run cmd.exe to redirect the stdin and legs that are passed with your commands.

  process.Start(...); process.StandardInput.WriteLine("Dir xxxxx"); process.StandardInput.WriteLine("Dir yyyyy"); process.StandardInput.WriteLine("Dir zzzzzz"); process.StandardInput.WriteLine("other command(s)"); 

Of course, you must remember to set up process star information to say that you want to redirect input:

  ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe); processStartInfo.CreateNoWindow = true; processStartInfo.ErrorDialog = false; processStartInfo.RedirectStandardInput = true; 
+2
source

Source: https://habr.com/ru/post/894652/


All Articles