Get path for install service

T

timburda

Scenario:

I have a service which has been installed in the service component
manager.

I am writing a second program (completely unrelated to the service)
which will run stand alone. I can find all of the services on the
machine with the following line of code:

System.ServiceProcess.ServiceController[] foo =
System.ServiceProcess.ServiceController.GetServices();

My question / problem is:

Is there a way for me to use the information returned by the method
call for an instance of a service (a particular service which I know
the name) to map that back to the pathname for the installed service?

If not, does anyone have any suggestions on a different approach to the
problem???

I am using C# (and accordingly .NET)


Thanks -

Tim Burda
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I'm not very clear of what you want in the first place, you can use the
registry to get extra info not supplied by the ServiceController class, use
the code below for that. Take a look in the registry and see what other nifo
you need.


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,
 
T

timburda

Ignacio -

Thanks for your reply. I discovered the registry settings shortly after
my post. But your code snippet was very useful as I hadn't got a chance
to write any code yet. I am looking for the information provided by
imagePath key. I was kind of surprised it was directory available
through properties of the ServiceController object, but the registry
information will work just as well ---- just a little more coding on my
part.

Again, thanks for your help and the code sample.

Tim
 
W

Willy Denoyette [MVP]

Scenario:

I have a service which has been installed in the service component
manager.

I am writing a second program (completely unrelated to the service)
which will run stand alone. I can find all of the services on the
machine with the following line of code:

System.ServiceProcess.ServiceController[] foo =
System.ServiceProcess.ServiceController.GetServices();

My question / problem is:

Is there a way for me to use the information returned by the method
call for an instance of a service (a particular service which I know
the name) to map that back to the pathname for the installed service?

If not, does anyone have any suggestions on a different approach to the
problem???

I am using C# (and accordingly .NET)


Thanks -

Tim Burda

Using System.Management...

string path = ServicePath("event log");
if (path != null) // found?
...




string ServicePath(string serviceName)
{
string ret = null;
ManagementObjectCollection Coll;
using(ManagementObjectSearcher Searcher = new
ManagementObjectSearcher("SELECT PathName from Win32_Service
where DisplayName =" + "\"" + serviceName + "\""))
{
foreach(ManagementObject service in Searcher.Get())
{
ret = service["PathName"].ToString();
}
}
return ret;
}

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top