Home All Groups Group Topic Archive Search About

Identify the Path of a Running Windows Service

Author
13 Oct 2006 8:07 AM
Ragu
Hi All

I have to get the path of a Running Windows Service from another application.
How to do this ?

Author
13 Oct 2006 1:37 PM
Gabriele G. Ponti
using System;
using System.Management;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main( string[] args )
        {
            Console.WriteLine( GetServicePath( "AudioSrv" ) );
            Console.ReadLine();
        }

        static string GetServicePath( string serviceName )
        {
            using( ManagementObjectSearcher mos = new
ManagementObjectSearcher( "SELECT PathName FROM Win32_Service WHERE Name =
\"" + serviceName + "\"" ) )
            {
                foreach( ManagementObject mo in mos.Get() )
                {
                    return Path.GetDirectoryName( mo[
"PathName" ].ToString() );
                }
            }
            throw new FileNotFoundException( "Service not found." );
        }

    }
}

Show quote
"Ragu" <R***@discussions.microsoft.com> wrote in message
news:8551FB4B-6602-401C-9340-FFFA0B877F9D@microsoft.com...
> Hi All
>
> I have to get the path of a Running Windows Service from another
> application.
> How to do this ?
>
>
Author
13 Oct 2006 2:23 PM
Ragu
Hi Gabriele

Thanx for your code.

It works fine for the services which are running from Windows\Systeme32
directory only.

For rest it returns an error "Illegal characters in path."

How to solve this issue.

Thanx in advance.

Regards
Ragu

Show quote
"Gabriele G. Ponti" wrote:

> using System;
> using System.Management;
> using System.IO;
>
> namespace ConsoleApplication1
> {
>     class Program
>     {
>         static void Main( string[] args )
>         {
>             Console.WriteLine( GetServicePath( "AudioSrv" ) );
>             Console.ReadLine();
>         }
>
>         static string GetServicePath( string serviceName )
>         {
>             using( ManagementObjectSearcher mos = new
> ManagementObjectSearcher( "SELECT PathName FROM Win32_Service WHERE Name =
> \"" + serviceName + "\"" ) )
>             {
>                 foreach( ManagementObject mo in mos.Get() )
>                 {
>                     return Path.GetDirectoryName( mo[
> "PathName" ].ToString() );
>                 }
>             }
>             throw new FileNotFoundException( "Service not found." );
>         }
>
>     }
> }
>
> "Ragu" <R***@discussions.microsoft.com> wrote in message
> news:8551FB4B-6602-401C-9340-FFFA0B877F9D@microsoft.com...
> > Hi All
> >
> > I have to get the path of a Running Windows Service from another
> > application.
> > How to do this ?
> >
> >
>
>
>
Author
13 Oct 2006 5:11 PM
Gabriele G. Ponti
Ragu,

I tested on my system with a service that doesn't reside in the
%WINDIR%\SYSTEM32 directory and it returned the path correctly.

Instead of

    return Path.GetDirectoryName( mo[ "PathName" ].ToString() );

try

    return mo[ "PathName" ].ToString();

and look at what value is returned.

    Gabriele

Show quote
"Ragu" <R***@discussions.microsoft.com> wrote in message
news:01139A22-025E-4C13-A0AE-0A421EF96168@microsoft.com...
> Hi Gabriele
>
> Thanx for your code.
>
> It works fine for the services which are running from Windows\Systeme32
> directory only.
>
> For rest it returns an error "Illegal characters in path."
>
> How to solve this issue.
>
> Thanx in advance.
>
> Regards
> Ragu
>
> "Gabriele G. Ponti" wrote:
>
>> using System;
>> using System.Management;
>> using System.IO;
>>
>> namespace ConsoleApplication1
>> {
>>     class Program
>>     {
>>         static void Main( string[] args )
>>         {
>>             Console.WriteLine( GetServicePath( "AudioSrv" ) );
>>             Console.ReadLine();
>>         }
>>
>>         static string GetServicePath( string serviceName )
>>         {
>>             using( ManagementObjectSearcher mos = new
>> ManagementObjectSearcher( "SELECT PathName FROM Win32_Service WHERE Name
>> =
>> \"" + serviceName + "\"" ) )
>>             {
>>                 foreach( ManagementObject mo in mos.Get() )
>>                 {
>>                     return Path.GetDirectoryName( mo[
>> "PathName" ].ToString() );
>>                 }
>>             }
>>             throw new FileNotFoundException( "Service not found." );
>>         }
>>
>>     }
>> }
>>
>> "Ragu" <R***@discussions.microsoft.com> wrote in message
>> news:8551FB4B-6602-401C-9340-FFFA0B877F9D@microsoft.com...
>> > Hi All
>> >
>> > I have to get the path of a Running Windows Service from another
>> > application.
>> > How to do this ?
>> >
>> >
>>
>>
>>

AddThis Social Bookmark Button