我想以编程方式运行所有命令,命令如下:
string mysql ="C:\Program Files\MySQL\MySQL Server 5.1\bin"
string command =" mysql.exe -u root -ppassword fabrica < c:/backup.sql";
我想用C#运行这两行,我该怎么做呢?
答案 0 :(得分:3)
编辑:现在我知道你想要做什么
以下是在方法
中创建的代码string binary = @"C:\MySQL\MySQL Server 5.0\bin\mysqldump.exe"
string arguments = @"-uroot -ppassword sample"
ProcessStartInfo PSI = new System.Diagnostics.ProcessStartInfo(binary, arguments);
PSI.RedirectStandardInput = true;
PSI.RedirectStandardOutput = true;
PSI.RedirectStandardError = true;
PSI.UseShellExecute = false;
Process p = System.Diagnostics.Process.Start(PSI);
Encoding encoding = p.StandardOutput.CurrentEncoding;
System.IO.StreamWriter SW = new StreamWriter(@"c:\backup.sql", false, encoding);
p.WaitOnExit();
string output = p.StandardOutput.ReadToEnd()
SW.Write(output)
SW.Close();
祝你好运!