BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Conversion Utility
 Rock Paper Scissors Game
 Random Number Generator
 DayLightTime
 BlogRoll
 Purchase Shareware
 Support
 Privacy

 


Sunday, October 07, 2007
Control and List Windows Services

As I whipped up my file synchronizing service application, to synchronize files between my computer and my NAS), I worked with the System.ServiceProcess Namespace. The System.ServiceProcess Namespace "provides classes that allow you to implement, install, and control Windows service applications".

My file synchronization needs did not require that my application service to be "running" all the time. In, fact I only needed the service to run under certain situations. It was easy enough to get the service written and installed and now I wanted to 'automatically' (programmatically) start and stop my service when certain conditions were met.

Entrance - the ServiceController Class. The ServiceController Class "Represents a Windows service and allows you to connect to a running or stopped service, manipulate it, or get information about it." In order to get acclimated to the ServiceController Class I created a simple application to list and display information about the installed services.


1using System;
2using System.Windows.Forms;
3using System.ServiceProcess;
4
5namespace Services1
6{
7 public partial class Form1 : Form
8 {
9 ServiceController controller;
10
11 public Form1()
12 {
13 controller = new ServiceController();
14 controller.MachineName = ".";
15 InitializeComponent();
16 lstServices.DisplayMember = "DisplayName";
17 lstServices.ValueMember = "ServiceName";
18 lstServices.DataSource = ServiceController.GetServices();
19 }

20
21 private void StartService(string servicename)
22 {
23 controller.ServiceName = servicename;
24 controller.Start();
25
26 }

27
28 private void PauseService(string servicename)
29 {
30 controller.ServiceName = servicename;
31 if (controller.CanPauseAndContinue)
32 controller.Pause();
33 }

34
35 private void StopService(string servicename)
36 {
37 controller.ServiceName = servicename;
38 if (controller.CanStop)
39 controller.Stop();
40 }

41
42
43 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
44 {
45 if (lstServices.SelectedItems.Count > 0)
46 {
47 try
48 {
49 controller.ServiceName = lstServices.SelectedValue.ToString();
50 textBox1.Text = String.Format(
51 "Service Name: {0}\r\nDisplay Name: {1}\r\nType: {2}\r\nStatus: {3}",
52 controller.ServiceName,
53 controller.DisplayName,
54 controller.ServiceType.ToString(),
55 controller.Status.ToString()
56 );
57 }

58 catch (Exception ex)
59 {
60 textBox1.Text = String.Format("Error: \r\n{0}",ex.Message);
61 }

62 }

63 else
64 {
65 textBox1.Clear();
66 }

67 }

68
69 private void btnStart_Click(object sender, EventArgs e)
70 {
71 StartService(lstServices.SelectedValue.ToString());
72 }

73
74 private void btnPause_Click(object sender, EventArgs e)
75 {
76 PauseService(lstServices.SelectedValue.ToString());
77 }

78
79 private void btnStop_Click(object sender, EventArgs e)
80 {
81 StopService(lstServices.SelectedValue.ToString());
82 }

83 }

84}

Labels: , ,

posted by Brad Prendergast at 3:12:00 PM
Comments:
Links to this post:

Create a Link

Recent Posts
 Let's Synchronize Some Files
 Compact that Virtual PC
 Is it Hammer Time?
 DSOFile and Summary Properties
 ShellExecuteEX and ShellExecuteInfo Revisited
 A ToolStripMenuItem and a Check
 Who is copying your web site?
 A little FileIconInit for the System Image list
 Code Monkey
 A DataGridView, DataGridViewCheckBoxColumn, DataGr...
Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
Powered by Blogger