我试图执行多个命令而不每次都创建新进程。基本上,我想启动DOS命令外壳,切换到MySQL命令外壳,然后执行命令。这是我调用该过程的方式(也在下面)。另外,如何处理命令中的“ \”?ExecuteCommand("mysql --user=root --password=sa casemanager", 100, false);ExecuteCommand(@"\. " + Environment.CurrentDirectory + @"\MySQL\CaseManager.sql", 100, true);private void ExecuteCommand(string Command, int Timeout, Boolean closeProcess){ ProcessStartInfo ProcessInfo; Process Process; ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command); ProcessInfo.CreateNoWindow = false; ProcessInfo.UseShellExecute = false; Process = Process.Start(ProcessInfo); Process.WaitForExit(Timeout); if (closeProcess == true) { Process.Close(); }}
3 回答
万千封印
TA贡献1891条经验 获得超3个赞
您可以重定向标准输入并使用StreamWriter对其进行写入:
Process p = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "cmd.exe";
info.RedirectStandardInput = true;
info.UseShellExecute = false;
p.StartInfo = info;
p.Start();
using (StreamWriter sw = p.StandardInput)
{
if (sw.BaseStream.CanWrite)
{
sw.WriteLine("mysql -u root -p");
sw.WriteLine("mypassword");
sw.WriteLine("use mydb;");
}
}
- 3 回答
- 0 关注
- 626 浏览
添加回答
举报
0/150
提交
取消