Browsing host services

  • Thread starter Thread starter r
  • Start date Start date
R

r

Hi,
I would like to create a dialog that shows all services in the
computer, much like services option in the control panel.
How can I traverse all services on the computer?
Thanks in advance
 
Hi,



Find the code below, you could also use ServiceController.GetServices but
you will be able to get all the properties of the services , like imagepath,
or description


cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation







using System;
using System.IO;
using System.Text;
using System.ServiceProcess;
using System.Web;
using System.Collections;
using Microsoft.Win32;

ArrayList ar = new ArrayList();
//StreamWriter writer = new StreamWriter( "c:\\testwww.txt", false);
string[] st = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
"System\\CurrentControlSet\\Services").GetSubKeyNames();
StringBuilder sb = new StringBuilder();
foreach( string key in st )
{

object imagePath;
object DisplayName;

if ( (imagePath =Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
"System\\CurrentControlSet\\Services\\" + key).GetValue( "ImagePath"))
== null )
continue;

if ( (DisplayName =Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
"System\\CurrentControlSet\\Services\\" + key).GetValue( "DisplayName"))
== null )
DisplayName = "Unknow";

sb.Append( "Service name=");
sb.Append( key );
sb.Append( " | ");

sb.Append( "DisplayName=");
sb.Append( DisplayName.ToString() );
sb.Append( " | ");

sb.Append( "Service image=");
sb.Append( imagePath.ToString() );

sb.Append( Environment.NewLine );


}

Cheers,
 
r said:
Hi,
I would like to create a dialog that shows all services in the
computer, much like services option in the control panel.
How can I traverse all services on the computer?
Thanks in advance

Use the System.Management namespace classes and WMI for these kind of
management tasks....

using System.Management;

....
// Get all properties of all services
SelectQuery query = new SelectQuery("SELECT * FROM Win32_Service");
using(ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query))
{
foreach(ManagementObject service in searcher.Get())
{
foreach(PropertyData pd in service.Properties)
Console.WriteLine("Property: {0}, Value: [{1}]",pd.Name,
pd.Value);
}
}

Willy.
 
Willy Denoyette said:
Use the System.Management namespace classes and WMI for these kind of
management tasks....

My problem with WMI is that not all functionality seems to match the WinAPI
for the same task,

Take for example changing the user that a service runs under (error checking
removed):

public static void ChangeUser(string name, string userName, string password)
{
ManagementObject service = new ManagementObject("Win32_Service=" + "\""
+ name + "\"");
ManagementBaseObject parameters = service.GetMethodParameters("Change");
parameters["StartName"] = userName;
parameters["StartPassword"] = password;
service.InvokeMethod("Change", parameters, null);
}

This works as you would expect but what it doesn't do which the WinAPI
function does do is give the user the "Logon as a Service" right, which
means you have to resort to other api's

bk
 
Willy Denoyette said:
Use the System.Management namespace classes and WMI for these kind of
management tasks....

My problem with WMI is that not all functionality seems to match the
WinAPI for the same task,

Take for example changing the user that a service runs under (error
checking removed):

public static void ChangeUser(string name, string userName, string
password) {
ManagementObject service = new ManagementObject("Win32_Service=" + "\""
+ name + "\"");
ManagementBaseObject parameters =
service.GetMethodParameters("Change");
parameters["StartName"] = userName;
parameters["StartPassword"] = password;
service.InvokeMethod("Change", parameters, null);
}

This works as you would expect but what it doesn't do which the WinAPI
function does do is give the user the "Logon as a Service" right, which
means you have to resort to other api's


Well that's exactly the kind of "convenience" which I don't like when using
the SC API's and the ServiceProcessInstaller class, why?
1. An account will automatically receive the "SeInteractiveLogonRight"
privilege when the service is installed, but it won't (and it can't) be
removed when the service is uninstalled. In a test environment you might end
with a bunch of accounts that are granted this privilege without any good
reason....
2. User privilege management and application management are separate tasks,
that means that you should configure your service accounts before you
install your application(s) aka. service(s). Service accounts should have
restricted privileges anyway, so picking an arbitrary user account to run as
a service is not good security practice anyway.

Note also that "User privilege management" is possible using
System.Management through the RSOP WMI namespace (XP and higher).

Willy.
 
Note also that "User privilege management" is possible using
System.Management through the RSOP WMI namespace (XP and higher).

Willy,

Are you able to modify the privilages using the RSOP classes, the help on
RSOP_UserPrivilegeRight indicates that the AccountList[] is readonly, and
there are no methods to add a new account to the collection, or is there a
different class I should be looking at?

thanks
bk
 
Back
Top