Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

332 lines
9.3 KiB

  1. namespace WindowsApplication1
  2. {
  3. using System;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. using System.ServiceProcess;
  7. /// <summary>
  8. /// Summary description for ServiceControllerManager.
  9. /// </summary>
  10. public class ServiceControllerManager
  11. {
  12. //The machine under control
  13. private string strMachineName;
  14. //selected service display name
  15. private string strDisplayName="";
  16. //The listBoxes used to show Services data
  17. private ListBox lstSrvRun, lstSrvStopped, lstSrvPaused,lstCurrent=null;
  18. /// <summary>
  19. /// Default empty contructor
  20. /// </summary>
  21. public ServiceControllerManager()
  22. {
  23. //The default machine name
  24. strMachineName= System.Environment.MachineName;
  25. }
  26. // Explicit constructor
  27. // <param name="tmpSrvRun"> </param>
  28. // pointer to the running services listbox
  29. // <param name="tmpSrvStopped"> </param>
  30. // pointer to the stopped services listbox
  31. // <param name="tmpSrvPaused"> </param>
  32. // pointer to the paused services listbox
  33. // <param name="tmpMachineName"> </param>
  34. // the machine name
  35. public ServiceControllerManager(ListBox tmpSrvRun, ListBox tmpSrvStopped, ListBox tmpSrvPaused,string tmpMachineName)
  36. {
  37. strMachineName=tmpMachineName;
  38. //Add the right event handlers for the listboxes
  39. //Get the pointers to the ListBoxes from the MainForm UI.
  40. //Assign the correspondant EventHandlers
  41. lstSrvPaused=tmpSrvPaused;
  42. lstSrvPaused.SelectedIndexChanged += new System.EventHandler(this.lstSrv_SelectedIndexChanged);
  43. lstSrvPaused.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstSrv_MouseDown);
  44. lstSrvStopped=tmpSrvStopped;
  45. lstSrvStopped.SelectedIndexChanged += new System.EventHandler(this.lstSrv_SelectedIndexChanged);
  46. lstSrvStopped.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstSrv_MouseDown);
  47. lstSrvRun=tmpSrvRun;
  48. lstSrvRun.SelectedIndexChanged += new System.EventHandler(this.lstSrv_SelectedIndexChanged);
  49. lstSrvRun.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lstSrv_MouseDown);
  50. LoadServices();
  51. }
  52. // Clear all the collections, arrays and eventhandlers
  53. public void Clear()
  54. {
  55. strMachineName="";
  56. strDisplayName="";
  57. if(lstSrvRun!=null)
  58. {
  59. //Clear the items in the list
  60. //Remove the event handlers
  61. lstSrvRun.Items.Clear();
  62. lstSrvRun.SelectedIndexChanged -= new System.EventHandler(this.lstSrv_SelectedIndexChanged);
  63. lstSrvRun.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.lstSrv_MouseDown);
  64. }
  65. if(lstSrvStopped!=null)
  66. {
  67. //Remove the event handlers
  68. lstSrvStopped.Items.Clear();
  69. lstSrvStopped.SelectedIndexChanged -= new System.EventHandler(this.lstSrv_SelectedIndexChanged);
  70. lstSrvStopped.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.lstSrv_MouseDown);
  71. }
  72. if(lstSrvPaused!=null)
  73. {
  74. lstSrvPaused.Items.Clear();
  75. lstSrvPaused.SelectedIndexChanged -= new System.EventHandler(this.lstSrv_SelectedIndexChanged);
  76. lstSrvPaused.MouseDown -= new System.Windows.Forms.MouseEventHandler(this.lstSrv_MouseDown);
  77. }
  78. }
  79. // Trap the name of the service selected by the user,
  80. // as well as the listbox the selected listbox
  81. public void lstSrv_SelectedIndexChanged(object sender, System.EventArgs e)
  82. {
  83. lstCurrent = (ListBox)sender;
  84. strDisplayName=lstCurrent.SelectedItem.ToString();
  85. }
  86. // PopUp the context menu
  87. public void lstSrv_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  88. {
  89. if(e.Button==System.Windows.Forms.MouseButtons.Right )
  90. {
  91. lstCurrent = (ListBox)sender;
  92. //Create a new contextMenu
  93. lstCurrent.ContextMenu=new System.Windows.Forms.ContextMenu();
  94. //And add the needed MenuItems
  95. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("&Start Service", new EventHandler(this.MenuStart)));
  96. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("S&top Service", new EventHandler(this.MenuStop)));
  97. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("&Pause Service", new EventHandler(this.MenuPause)));
  98. lstCurrent.ContextMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("S&how Service Info", new EventHandler(this.ShowServiceInfo)));
  99. //check which menu should be active
  100. if(lstCurrent.Equals(lstSrvRun) )
  101. {
  102. lstCurrent.ContextMenu.MenuItems[0].Enabled=false;
  103. }
  104. else if(lstCurrent.Equals(lstSrvStopped ))
  105. {
  106. lstCurrent.ContextMenu.MenuItems[1].Enabled=false;
  107. lstCurrent.ContextMenu.MenuItems[2].Enabled=false;
  108. }
  109. else
  110. {
  111. lstCurrent.ContextMenu.MenuItems[2].Enabled=false;
  112. }
  113. lstCurrent.ContextMenu.Show(lstCurrent ,new Point(e.X,e.Y));
  114. }
  115. }
  116. private void MenuStart(object sender, EventArgs e)
  117. {
  118. System.ServiceProcess.ServiceController tmpSC=new System.ServiceProcess.ServiceController();
  119. tmpSC.MachineName=strMachineName;
  120. tmpSC.DisplayName=strDisplayName;
  121. try
  122. {
  123. //When a service is paused, it has to continue.
  124. //Start in this case is not possible.
  125. if(lstCurrent.Equals(lstSrvPaused))
  126. tmpSC.Continue();
  127. else
  128. tmpSC.Start();
  129. System.Threading.Thread.Sleep(500);
  130. //Loop while the service is pending the continue state
  131. while(tmpSC.Status== ServiceControllerStatus.ContinuePending )
  132. {
  133. Application.DoEvents() ;
  134. }
  135. //after starting the service, refresh the listBoxes
  136. if(tmpSC.Status==ServiceControllerStatus.Running )
  137. {
  138. lstSrvRun.Items.Add(lstCurrent.SelectedItem.ToString());
  139. lstCurrent.Items.Remove (lstCurrent.SelectedItem.ToString());//(lstCurrent.SelectedIndex);
  140. lstCurrent.Refresh();
  141. lstSrvRun.Refresh();
  142. }
  143. //Perhaps it could not start...
  144. else
  145. {
  146. MessageBox.Show(tmpSC.ServiceName + " Cannot be started");
  147. }
  148. }
  149. catch
  150. //Do you have enough permissions to start this service ?
  151. {
  152. MessageBox.Show("Service: " + strDisplayName + " Could not be started ! ");
  153. }
  154. }
  155. private void MenuStop(object sender, EventArgs e)
  156. {
  157. //Stop a service
  158. System.ServiceProcess.ServiceController tmpSC=new System.ServiceProcess.ServiceController();
  159. tmpSC.MachineName=strMachineName;
  160. tmpSC.DisplayName=strDisplayName;
  161. try
  162. {
  163. if(tmpSC.CanStop)
  164. {
  165. try
  166. {
  167. tmpSC.Stop();
  168. System.Threading.Thread.Sleep(500);
  169. while(tmpSC.Status == ServiceControllerStatus.StopPending)
  170. {
  171. Application.DoEvents();
  172. }
  173. lstSrvStopped.Items.Add(lstCurrent.SelectedItem.ToString());
  174. lstCurrent.Items.Remove(lstCurrent.SelectedItem.ToString());
  175. lstCurrent.Refresh();
  176. lstSrvStopped.Refresh();
  177. }
  178. catch
  179. {
  180. MessageBox.Show("Service could not be stopped !");
  181. }
  182. }
  183. else
  184. {
  185. MessageBox.Show("The service: " + tmpSC.DisplayName + " is not allowed to be stopped !");
  186. }
  187. }
  188. catch
  189. {
  190. MessageBox.Show("Select a service and try again!");
  191. }
  192. }
  193. private void MenuPause(object sender, EventArgs e)
  194. {
  195. //Pause a service
  196. System.ServiceProcess.ServiceController tmpSC=new System.ServiceProcess.ServiceController();
  197. tmpSC.MachineName=strMachineName;
  198. tmpSC.DisplayName=strDisplayName;
  199. try
  200. {
  201. //And refresh the listBoxes
  202. tmpSC.Pause();
  203. lstSrvPaused.Items.Add(lstCurrent.SelectedItem.ToString());
  204. lstCurrent.Items.Remove(lstCurrent.SelectedItem.ToString());
  205. lstCurrent.Refresh();
  206. lstSrvPaused.Refresh();
  207. }
  208. catch
  209. {
  210. //or may be not possible to stop the service...
  211. MessageBox.Show("Service: " + strDisplayName + " Could not be paused !");
  212. }
  213. }
  214. // Load all services on the machine
  215. public void LoadServices()
  216. {
  217. if(!strMachineName.Equals(""))
  218. {
  219. ServiceController[]arrSrvCtrl;
  220. try
  221. {
  222. //That's enough to get all the services running on the machine
  223. arrSrvCtrl= ServiceController.GetServices(strMachineName);
  224. //Clear all the collections
  225. lstSrvRun.Items.Clear();
  226. lstSrvPaused.Items.Clear();
  227. lstSrvStopped.Items.Clear();
  228. //Fill up all the listBoxes
  229. foreach(ServiceController tmpSC in arrSrvCtrl)
  230. {
  231. if(tmpSC.Status==ServiceControllerStatus.Running )
  232. lstSrvRun.Items.Add (tmpSC.DisplayName );
  233. else if(tmpSC.Status== ServiceControllerStatus.Paused )
  234. lstSrvPaused.Items.Add(tmpSC.DisplayName);
  235. else
  236. lstSrvStopped.Items.Add(tmpSC.DisplayName);
  237. }
  238. //Sort them alphabeticaly
  239. lstSrvPaused.Sorted=lstSrvRun.Sorted=lstSrvStopped.Sorted =true;
  240. }
  241. catch
  242. {
  243. MessageBox.Show("Couldn't load the services !");
  244. }
  245. }
  246. }
  247. // Show the process info window
  248. public void ShowServiceInfo(object sender, EventArgs e)
  249. {
  250. System.ServiceProcess.ServiceController tmpSC;
  251. tmpSC=new System.ServiceProcess.ServiceController();
  252. tmpSC.MachineName=strMachineName;
  253. tmpSC.DisplayName=strDisplayName;
  254. ServiceInfo si = new ServiceInfo(tmpSC);
  255. si.Show();
  256. }
  257. }
  258. }