Problem, executing commands in cmd using c#
Posted
by srk
on Stack Overflow
See other posts from Stack Overflow
or by srk
Published on 2010-06-14T05:28:47Z
Indexed on
2010/06/14
7:12 UTC
Read the original article
Hit count: 224
c#
I need to execute the below command in command prompt.
C:\MySQL\MySQL Server 5.0\bin>mysql -uroot -ppassword < d:/admindb/aar.sql
When i do this manually in cmd, i am getting my results.
Now i am trying to do this programatically, to execute it in cmd from c# code.
I am using the below code to do it. I am not getting any errors and Result !!!
When i debug, i get the value of string commandLine as below,
"\"C:\\MySQL\\MySQL Server 5.0\\bin\\\" -uroot -ppassword > \"D:/admindb/AAR12.sql"
I guess the problem is with this string, passed to cmd. How to solve this ??.
public void Execute()
{
string commandLine = "\"" + MySqlCommandPath + "\"" + " -u" + DbUid + " -p" + DbPwd + " > " + "\"" + Path.Combine(Path_Backup, FileName_Backup + ExcID + ".sql");
System.Diagnostics.ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo("cmd.exe");
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
System.Diagnostics.Process p = System.Diagnostics.Process.Start(PSI);
System.IO.StreamWriter SW = p.StandardInput;
System.IO.StreamReader SR = p.StandardOutput;
SW.WriteLine(commandLine);
SW.Close();
}
© Stack Overflow or respective owner