Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sir,

Can you help me with code to design an application to list and control all services in current machine/or remote machine?

nimsss
Posted
Updated 10-Jan-11 23:13pm
v2

Here is an excellent article:
Command Line Windows Services Manager[^]

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-11 11:11am    
Useful - a 5.
Espen Harlinn 11-Jan-11 11:19am    
Thanks SAKryukov!
Below code I found useful from THIS[^] thread would be a way for you to retrieve all the service from current machine.
RegistryKey rk = Registry.LocalMachine;
rk = rk.OpenSubKey(@"SYSTEM\CurrentControlSet\Services");
foreach (string keyName in  rk.GetSubKeyNames())
{
Console.WriteLine(keyName);
}


Hope it helps.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-11 11:12am    
Hiren, this is not portable/supportable enough. Microsoft provided more civilized way; please see my answer.
Pretty much the same way as with Process (see my answer to your question on Processes.

Add reference to assembly System.ServiceProcess.


C#
using System.ServiceProcess;

//..

void DoWhatYouWantWithYourService(ServiceController controller) {
    /* some code */
}

foreach (ServiceController controller in ServiceController.GetServices())
    DoWhatYouWantWithYourService(controller);


There is also an overloaded version of GetServices for specified machine:

C#
public static ServiceController[] GetServices(
	string machineName
)


This is a complete answer. Who knows how do you want to "control" your services?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jan-11 10:24am    
@Nimisha: And yes, don't forget to put cursor on ServiceController and hit F1 -- will help :-)
Espen Harlinn 11-Jan-11 11:01am    
5+ Good answer
Hiren solanki 12-Jan-11 4:02am    
Good answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900