How to execute a command in CMD or Powershell, passing the commands via C#?

Asked

Viewed 1,434 times

1

I need to execute a command on CMD that I pass the parameters via C# for example:

"C:\Program Files\MySQL\MySQL Workbench 8.0 CE\mysqldump.exe" --column-statistics=0 -uroot -p1234 -hservidor -P3306 bancomysql  > D:\banco.sql

but I don’t want to run this command via . bat or type myself in CMD. I would like C# to pass this command and open the CMD or the Shell to do so.

I don’t know how I would do this command via Shell but I heard it would be easier to call the Shell via C#

That is, in the program C# I want to press a button that calls this command and open the CMD or the Shell and run this command for me.

  • the path is right ? normally I use mysqldump.exe from where mysql is installed. not from Workbench

  • yes the command works directly from a . bat or CMD, but I don’t know how to do this in C# directly

  • C# I don’t know how to help anymore, but check out this link here: https://stackoverflow.com/questions/5519328/executing-file-in-c-sharp

1 answer

4


C# to open and CMD and run the command:

System.Diagnostics.Process.Start("cmd.exe", "/C \"C:\\Program Files\\MySQL\\MySQL Workbench 8.0 CE\\mysqldump.exe\" --column-statistics=0 -uroot -p1234 -hservidor -P3306 bancomysql  > D:\banco.sql");

C# to open Powershell:

System.Diagnostics.Process.Start("powershell.exe", "/C \"C:\\Program Files\\MySQL\\MySQL Workbench 8.0 CE\\mysqldump.exe\" --column-statistics=0 -uroot -p1234 -hservidor -P3306 bancomysql  > D:\banco.sql");

The important 'and the "/C" command that copies the entire string in front and pastes in the CMD/Shell.

  • Thank you so much, I’ve been trying to figure out how to do this for a long time!!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.