加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

通过C#的Process启动Nginx

发布时间:2020-12-16 01:19:57 所属栏目:百科 来源:网络整理
导读:背景:因为一个特殊的需求,需要在WPF程序中嵌入Nginx,并能通过WPF启动停止Nginx服务。直接Process.Strat("nginx.exe",path);无法正常启动,折腾半天找到的一条方法,记录分享。 1 private void LaunchNginx() 2 { 3 try 4 { 5 ProcessStartInfo info = new

背景:因为一个特殊的需求,需要在WPF程序中嵌入Nginx,并能通过WPF启动停止Nginx服务。直接Process.Strat("nginx.exe",path);无法正常启动,折腾半天找到的一条方法,记录分享。

 1 private void LaunchNginx()
 2         {
 3             try
 4             {
 5                 ProcessStartInfo info = new ProcessStartInfo();
 6                 info.FileName = "cmd.exe";
 7                 info.UseShellExecute = false;
 8                 info.RedirectStandardInput = true;
 9                 info.RedirectStandardOutput = true;
10                 info.CreateNoWindow = true;
11                 Process p = new Process();
12                 p.StartInfo = info;
13                 p.Start();
14                 //p.PriorityClass = ProcessPriorityClass.RealTime;
15                 string cmdStr = "cd " + System.AppDomain.CurrentDomain.BaseDirectory + "nginx-1.16.0";
16                 cmdStr = cmdStr.Replace("","\");
17                 p.StandardInput.WriteLine(cmdStr);
18                 //cmd启动程序
19                 p.StandardInput.WriteLine("nginx.exe");
20                 //Thread.Sleep(2000);
21                 p.StandardInput.WriteLine("exit");
22             }
23             catch (Exception ex)
24             {
25                 //throw;
26                 Logger.WriteToError(ex,"Nginx服务启动失败");
27             }
28 
29         }

如图Nginx起来了~

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读