使用.NET相同的进程中执行多个命令行 [英] Execute multiple command lines with the same process using .NET
本文介绍了使用.NET相同的进程中执行多个命令行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想执行多个命令,无需每次都创建新的进程。基本上,我要开始的DOS命令外壳程序,切换到MySQL命令外壳,并执行命令。以下是我调用的程序(下文)。另外,我怎么在命令处理\的?
的ExecuteCommand(MySQL的--user =根 - 密码= SA casemanager,100,FALSE);
的ExecuteCommand(@\。+ Environment.CurrentDirectory + @\ MySQL的\ CaseManager.sql,100,真正的);
私人无效的ExecuteCommand(字符串命令,INT超时,布尔closeProcess)
{
的ProcessStartInfo ProcessInfo;
工艺过程;
ProcessInfo =新的ProcessStartInfo(cmd.exe的,/ C+指令);
ProcessInfo.CreateNoWindow = FALSE;
ProcessInfo.UseShellExecute = FALSE;
流程=的Process.Start(ProcessInfo);
Process.WaitForExit(超时);
如果(closeProcess ==真){Process.Close(); }
}
解决方案
您可以重定向标准输入和使用的StreamWriter写的:
进程p =新工艺();
的ProcessStartInfo信息=新的ProcessStartInfo();
info.FileName =cmd.exe的;
info.RedirectStandardInput = TRUE;
info.UseShellExecute = FALSE;
p.StartInfo =信息;
p.Start();
使用(StreamWriter的SW = p.StandardInput)
{
如果(sw.BaseStream.CanWrite)
{
sw.WriteLine(MySQL的ü根 - P);
sw.WriteLine(输入mypassword);
sw.WriteLine(使用MYDB;);
}
}
I'm trying to execute multiple commands without create a new process each time. Basically, I want to start the DOS command shell, switch to the MySQL command shell, and execute a command. Here's how I am calling the procedure (also below). Also, how do I handle the "\"'s in the command?
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(); }
}
解决方案
You can redirect standard input and use a StreamWriter to write to it:
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;");
}
}
这篇关于使用.NET相同的进程中执行多个命令行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文