C#调用cmd 脚本实例
1.实例1
public static void TestOne() { Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); p.StandardInput.WriteLine("systeminfo" + "&exit"); p.StandardInput.AutoFlush = true; string result = p.StandardOutput.ReadToEnd(); p.WaitForExit(); p.Close(); Console.WriteLine(result); }
显示结果:
调用系统命令:
p.StandardInput.WriteLine("systeminfo" + " &exit"); p.StandardInput.WriteLine("ipconfig" + " &exit"); p.StandardInput.WriteLine("ping www.weilanliuxue.cn" + " &exit");
调用其他工具命令:(备份MySql数据库)
p.StartInfo.WorkingDirectory = @"E:\mysql-5.6.26-winx64\bin"; string strSql = "mysqldump --quick --host=localhost -u root -p123 test > d:\\test_20151227110010.sql";