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 All
>
> I have to get the path of a Running Windows Service from another
> application.
> How to do this ?
Ragu - 13 Oct 2006 15:23 GMT
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
> using System;
> using System.Management;
[quoted text clipped - 33 lines]
> > application.
> > How to do this ?
Gabriele G. Ponti - 13 Oct 2006 18:11 GMT
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
> Hi Gabriele
>
[quoted text clipped - 50 lines]
>> > application.
>> > How to do this ?