折腾了半天,可以正常运行.记录下,以便后续需要的时候可以直接复制使用.
代码语言:javascript复制void stratEMQX()
{
string PathMqtt = ConfigurationManager.AppSettings["PathMqtt"];
string str;//= Console.ReadLine();
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
p.Start();//启动程序
str = PathMqtt "emqx.cmd start ";
p.StandardInput.WriteLine(str "&exit"); //向cmd窗口发送输入信息
string output = "尝试启动中......";
Log.Information("线程:{ThreadId:D3} MQtt命令:{output}", Thread.CurrentThread.ManagedThreadId, str);
p.WaitForExit();//等待程序执行完退出进程
p.Close();
p.Start();//启动程序
str = PathMqtt "emqx_ctl.cmd status ";
p.StandardInput.WriteLine(str "&exit");
p.StandardInput.AutoFlush = true;
Log.Information("线程:{ThreadId:D3} MQtt命令:{output}", Thread.CurrentThread.ManagedThreadId, str);
output = p.StandardOutput.ReadToEnd();
Log.Warning("线程:{ThreadId:D3} MQtt服务器启动:{output}", Thread.CurrentThread.ManagedThreadId, output);
if (output.Contains("is running") || output.Contains("is started"))
{
output = "MQTT服务器已启动...";
Log.Warning("线程:{ThreadId:D3} MQtt服务器启动:{output}", Thread.CurrentThread.ManagedThreadId, output);
}
else
{
output = "MQTT服务器启动失败...";
Log.Error("线程:{ThreadId:D3} MQtt服务器启动:{output}", Thread.CurrentThread.ManagedThreadId, output);
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();
}