首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >[C#] 操作CMD笔记

[C#] 操作CMD笔记

作者头像
科控物联
发布2022-03-29 18:52:58
发布2022-03-29 18:52:58
4540
举报
文章被收录于专栏:科控自动化科控自动化

折腾了半天,可以正常运行.记录下,以便后续需要的时候可以直接复制使用.

代码语言: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();
 
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-12-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 科控物联 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档