Identify the Path of a Running Windows Service

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All

I have to get the path of a Running Windows Service from another application.
How to do this ?
 
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." );
}

}
}
 
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 said:
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 said:
Hi All

I have to get the path of a Running Windows Service from another
application.
How to do this ?
 
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

Ragu said:
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 said:
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 said:
Hi All

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