|
|
|
In .Net using "System.ServiceProcess" namespace now its so easy to work with windows services. This article will show you how easy it is to get list of all running service or to check status of a single service by name. To control (i.e start/stop/paush) or retrive information of any service you need to work with "System.ServiceProcess.ServiceController" class.
Step-By-Step Example
- Create a new windows application project - Add new command button on the form (Button1) - Now right click on the references folder (mostly right side in the Solution Explorer) of your project and click "add reference...", Select "System.ServiceProcess.dll", click ok - Add the following code in your form and then press F5 to run the project, click on the button to see the demo
Note : You can not simply use Import Statement to add System.ServiceProcess reference so you must add it using "Add Reference..." |
Click here to copy the following block | void ShowServices(string MCName, bool OnlyShowRunning) { //System.ServiceProcess.ServiceController[] arrSc; int i=0; System.ServiceProcess.ServiceController[] arrSc= System.ServiceProcess.ServiceController.GetServices(MCName); System.Text.StringBuilder sb = new System.Text.StringBuilder(); for ( i = 0; i <= arrSc.GetUpperBound(0); i++) { if (OnlyShowRunning == true) { if (arrSc[i].Status == System.ServiceProcess.ServiceControllerStatus.Running) { sb.Append("[" + GetServiceStatusString(arrSc[i]) + "] " + arrSc[i].ServiceName + " (" + arrSc[i].DisplayName + ")" + "\r\n"); } } else { sb.Append("[" + GetServiceStatusString(arrSc[i]) + "] " + arrSc[i].ServiceName + " (" + arrSc[i].DisplayName + ")" + "\r\n"); } } MessageBox.Show(sb.ToString()); }
void ShowServiceStatus(string MCName, string ServiceName) { System.ServiceProcess.ServiceController sc = new System.ServiceProcess.ServiceController(); sc.MachineName = MCName; sc.ServiceName = ServiceName; MessageBox.Show("MSSQLSERVER Service = > " + GetServiceStatusString(sc)); }
string GetServiceStatusString(System.ServiceProcess.ServiceController sc) { string s="";
if (sc.Status== System.ServiceProcess.ServiceControllerStatus.Running) { s= "Running"; } else if (sc.Status == System.ServiceProcess.ServiceControllerStatus.ContinuePending) { s="ContinuePending"; } else if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Paused) { s="Paused"; } else if (sc.Status == System.ServiceProcess.ServiceControllerStatus.PausePending) { s="PausePending"; } else if (sc.Status == System.ServiceProcess.ServiceControllerStatus.Stopped) { s="Stopped"; } else if (sc.Status == System.ServiceProcess.ServiceControllerStatus.StopPending) { s="StopPending"; }
return s; }
private void button1_Click(object sender, System.EventArgs e) { ShowServiceStatus("localhost", "MSSQLSERVER"); ShowServices("localhost",true); } |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|