6

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'm making small app what will run g++. Is this now correct?:

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

Command for compiling:

g++ -c C:\Alps\here_is_projectname\Debug\Main.cpp -o main.o

4 Answers 4

7
cmd.exe /k <command>
cmd.exe /c <command>

Are both valid.

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

If you're looking 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();
Sign up to request clarification or add additional context in comments.

10 Comments

/k is better for me. Thanks. And one more small question: will cmd start in C# application path (in vs default mydocs.....\bin\debug)? I need more commands so /c is bad for me.
@FrewCen: To be honest, I don't recall. However, you can modify the ProcessStartInfo.WorkingPath to have it run where ever you'd like.
@FrewCen: My apologies, I didn't include the /c flag in the StartInfo.Arguments assignment.
ok, but can you please look on ful command and edit your code, because you have there many '"' and i don't know how to edit that code in that line. Where can I add -c and -o ?
@FrewCen: Why don't you call g++.exe directly then out of curiosity? Why do you pipe the command through cmd.exe first?
|
7

Yes, you can pass in the command line using the "/C" switch:

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

1 Comment

@chibacity: Sorry, edited your answer not mine. My apologies.
3

You can also do like the following....

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

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

For example..

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

Comments

2

You can launch cmd.exe redirect the stdin and feet that stream 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 should remeber to set your process star info to say you want redirect input:

 ProcessStartInfo processStartInfo = new ProcessStartInfo("cmd.exe);
        processStartInfo.CreateNoWindow = true;

        processStartInfo.ErrorDialog = false;

        processStartInfo.RedirectStandardInput = true;

1 Comment

@FrewCen process in my example is an instance of process, not the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.