Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Identify the Path of a Running Windows Service

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ragu - 13 Oct 2006 09:07 GMT
Hi All

I have to get the path of a Running Windows Service from another application.
How to do this ?
Gabriele G. Ponti - 13 Oct 2006 14:37 GMT
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 ?

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.