NginxHandler.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace ServerControl
  11. {
  12. class NginxHandler
  13. {
  14. Boolean isExecuted = false;
  15. string Path = "";
  16. Label statusLabel;
  17. RichTextBox outputText;
  18. public NginxHandler(string Path, Label Status)
  19. {
  20. this.Path = Path;
  21. statusLabel = Status;
  22. if (File.Exists(Path + "\\logs\\nginx.pid"))
  23. {
  24. statusLabel.Invoke((Action)delegate { statusLabel.Text = "Подключено"; });
  25. isExecuted = true;
  26. }
  27. }
  28. public void Connect()
  29. {
  30. if (isExecuted) return;
  31. isExecuted = true;
  32. Process myProcess = new Process();
  33. myProcess.StartInfo.FileName = "cmd.exe";
  34. myProcess.StartInfo.RedirectStandardInput = true;
  35. myProcess.StartInfo.UseShellExecute = false;
  36. myProcess.EnableRaisingEvents = true;
  37. myProcess.StartInfo.CreateNoWindow = false;
  38. statusLabel.Invoke((Action)delegate { statusLabel.Text = "Подключено"; });
  39. myProcess.Start();
  40. using (StreamWriter sw = myProcess.StandardInput)
  41. {
  42. if (sw.BaseStream.CanWrite)
  43. {
  44. sw.WriteLine("cd " + Path);
  45. sw.WriteLine("start nginx");
  46. }
  47. }
  48. }
  49. public void Disconnect(object sender, EventArgs e)
  50. {
  51. Disconnect();
  52. }
  53. public void Disconnect()
  54. {
  55. if (!isExecuted) return;
  56. isExecuted = false;
  57. Process myProcess = new Process();
  58. myProcess.StartInfo.FileName = "cmd.exe";
  59. myProcess.StartInfo.RedirectStandardInput = true;
  60. myProcess.StartInfo.UseShellExecute = false;
  61. myProcess.EnableRaisingEvents = true;
  62. myProcess.StartInfo.CreateNoWindow = false;
  63. statusLabel.Invoke((Action)delegate { statusLabel.Text = "Отключено"; });
  64. myProcess.Start();
  65. using (StreamWriter sw = myProcess.StandardInput)
  66. {
  67. if (sw.BaseStream.CanWrite)
  68. {
  69. sw.WriteLine("cd " + Path);
  70. sw.WriteLine("nginx -s stop");
  71. }
  72. }
  73. myProcess.WaitForExit();
  74. }
  75. }
  76. }