[SOLVED] how i can execute CMD command in c# console application?

Issue

It’s very simple to make a mysqldump in cmd on windows, simply:

Open cmd and put type mysqldump uroot ppassword database > c:/data.sql

This results in an SQL dump file for the desired database.

I’m writing a console application so I may run this command:

-uroot -ppass databse  > location\data.sql

I tried the following code to no avail:

System.Diagnostics.ProcessStartInfo procStartInfo =
    new System.Diagnostics.ProcessStartInfo("cmd", "/c " + cmd); 

How might I start a cmd process and send my command successfully?

Solution

Is there a reason why you don’t call mysqldump directly?

ProcessStartInfo procStartInfo = 
    new ProcessStartInfo("mysqldump", "uroot ppassword databse > c:/data.sql");

If there is a reason, your code should look like this:

ProcessStartInfo procStartInfo = 
    new ProcessStartInfo("cmd", 
        "/c \"mysqldump uroot ppassword databse > c:/data.sql\"");

Changes:

  • You where missing “mysqldump” in your cmd variable.
  • You should put the command to be executed in the command line into quotes.

Answered By – Daniel Hilgarth

Answer Checked By – David Goodson (BugsFixing Volunteer)

Leave a Reply

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