通过C#的Process启动Nginx

背景:因为一个特殊的需求,需要在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起来了~

分享图片

相关文章

项目中经常遇到CSV文件的读写需求,其中的难点主要是CSV文件...
简介 本文的初衷是希望帮助那些有其它平台视觉算法开发经验的...
这篇文章主要简单记录一下C#项目的dll文件管理方法,以便后期...
在C#中的使用JSON序列化及反序列化时,推荐使用Json.NET——...
事件总线是对发布-订阅模式的一种实现,是一种集中式事件处理...
通用翻译API的HTTPS 地址为https://fanyi-api.baidu.com/api...