470,624 Members | 2,393 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,624 developers. It's quick & easy.

Get path for install service

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.GetService s();

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

Nov 17 '05 #1
5 13129
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").GetSubKeyNa mes();
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ti******@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.GetService s();

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

Nov 17 '05 #2
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").GetSubKeyNa mes();
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
<ti******@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.GetService s();

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

Nov 17 '05 #3
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

Nov 17 '05 #4
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

Nov 17 '05 #5

<ti******@hotmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.GetService s();

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.
Nov 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Jeremy S. | last post: by
7 posts views Thread by hufaunder | last post: by
6 posts views Thread by Jean-Marc Blaise | last post: by
2 posts views Thread by Gregor Horvath | last post: by
11 posts views Thread by cybervigilante | last post: by
12 posts views Thread by Anil Gupte | last post: by
reply views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post: by
reply views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgRGVzYXJyb2xsbw==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.