Guest User

Untitled

a guest
Feb 17th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Management;
  6. using System.Net.NetworkInformation;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10.  
  11. namespace ServiceManager
  12. {
  13. class Service
  14. {
  15. private readonly string _ip;
  16. private readonly string _user;
  17. private readonly string _password;
  18. private readonly string _serviceName;
  19.  
  20. public string ProcessOutput { get; set; }
  21.  
  22. public string Status { get; set; }
  23.  
  24.  
  25. public Service(string ip, string user, string passwrod, string serviceName)
  26. {
  27. this._ip = ip;
  28. this._user = user;
  29. this._password = passwrod;
  30. this._serviceName = serviceName;
  31. }
  32.  
  33. private ProcessStartInfo CreateStartInfo()
  34. {
  35. return new ProcessStartInfo
  36. {
  37. FileName = "cmd.exe",
  38. WindowStyle = ProcessWindowStyle.Hidden,
  39. CreateNoWindow = true,
  40. RedirectStandardOutput = true,
  41. UseShellExecute = false
  42. };
  43. }
  44.  
  45. private Process CreateProcess(string action)
  46. {
  47. var parametrs = _ip + " " + _user + " " + _password + " " + action + " " + _serviceName;
  48. Process myProcess = new Process{StartInfo = CreateStartInfo()};
  49. myProcess.StartInfo.Arguments = @"/c" + Environment.CurrentDirectory + "/controlservice.bat " + parametrs;
  50. return myProcess;
  51. }
  52.  
  53. public bool Stop()
  54. {
  55. var myProcess = CreateProcess("stop");
  56. try
  57. {
  58. myProcess.Start();
  59. ProcessOutput = myProcess.StandardOutput.ReadToEnd();
  60. if (ProcessOutput.Contains("успешно"))
  61. {
  62. return true;
  63. }
  64. else
  65. {
  66. return false;
  67. }
  68. }
  69. catch (Exception e)
  70. {
  71. ProcessOutput = e.Message;
  72. return false;
  73. }
  74. }
  75.  
  76. public bool Start()
  77. {
  78. var myProcess = CreateProcess("start");
  79. try
  80. {
  81. myProcess.Start();
  82. ProcessOutput = myProcess.StandardOutput.ReadToEnd();
  83. if (ProcessOutput.Contains("успешно"))
  84. {
  85. return true;
  86. }
  87. else
  88. {
  89. return false;
  90. }
  91. }
  92. catch (Exception e)
  93. {
  94. ProcessOutput = e.Message;
  95. return false;
  96. }
  97. }
  98.  
  99. public string GetPingStatus(string ip)
  100. {
  101. Ping ping = new Ping();
  102. return ping.Send(ip, 2000)?.Status.ToString();
  103. }
  104.  
  105. private void GetStatus() {
  106.  
  107. if (!string.Equals(GetPingStatus(_ip).ToLower(), GetPingStatus("127.0.0.1").ToLower()))
  108. {
  109. Status = "Unknown";
  110. }
  111. var connectionsOptions = new ConnectionOptions
  112. {
  113. Username = @".\" + _user,
  114. Password = _password,
  115. EnablePrivileges = true,
  116. Impersonation = ImpersonationLevel.Impersonate
  117. };
  118. var managementScope = new ManagementScope(string.Format($@"\\{_ip}\root\cimv2", connectionsOptions))
  119. {
  120. Options = connectionsOptions
  121. };
  122. managementScope.Connect();
  123. var query = new ObjectQuery(string.Format($@"Select * from Win32_Service where name = '{_serviceName}'"));
  124. var seacher = new ManagementObjectSearcher(managementScope, query);
  125. var servicesCollection = seacher.Get();
  126. Status=servicesCollection.OfType<ManagementObject>().FirstOrDefault()?.GetPropertyValue("State").ToString();
  127. }
  128. }
  129. }
Add Comment
Please, Sign In to add comment