Get path for install service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • timburda@hotmail.com

    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.ServiceP rocess.ServiceC ontroller[] foo =
    System.ServiceP rocess.ServiceC ontroller.GetSe rvices();

    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

  • Ignacio Machin \( .NET/ C# MVP \)

    #2
    Re: Get path for install service

    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 ServiceControll er 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.Local Machine.OpenSub Key(
    "System\\Curren tControlSet\\Se rvices").GetSub KeyNames();
    StringBuilder sb = new StringBuilder() ;
    foreach( string key in st )
    {

    object imagePath;
    object DisplayName;

    if ( (imagePath =Microsoft.Win3 2.Registry.Loca lMachine.OpenSu bKey(
    "System\\Curren tControlSet\\Se rvices\\" + key).GetValue( "ImagePath" ))
    == null )
    continue;

    if ( (DisplayName =Microsoft.Win3 2.Registry.Loca lMachine.OpenSu bKey(
    "System\\Curren tControlSet\\Se rvices\\" + key).GetValue( "DisplayNam e"))
    == null )
    DisplayName = "Unknow";

    sb.Append( "Service name=");
    sb.Append( key );
    sb.Append( " | ");

    sb.Append( "DisplayName=") ;
    sb.Append( DisplayName.ToS tring() );
    sb.Append( " | ");

    sb.Append( "Service image=");
    sb.Append( imagePath.ToStr ing() );

    sb.Append( Environment.New Line );
    }


    cheers,

    --
    Ignacio Machin,
    ignacio.machin AT dot.state.fl.us
    Florida Department Of Transportation


    <timburda@hotma il.com> wrote in message
    news:1114009493 .461513.85100@z 14g2000cwz.goog legroups.com...[color=blue]
    > 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.ServiceP rocess.ServiceC ontroller[] foo =
    > System.ServiceP rocess.ServiceC ontroller.GetSe rvices();
    >
    > 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
    >[/color]


    Comment

    • Ignacio Machin \( .NET/ C# MVP \)

      #3
      Re: Get path for install service

      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 ServiceControll er 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.Local Machine.OpenSub Key(
      "System\\Curren tControlSet\\Se rvices").GetSub KeyNames();
      StringBuilder sb = new StringBuilder() ;
      foreach( string key in st )
      {

      object imagePath;
      object DisplayName;

      if ( (imagePath =Microsoft.Win3 2.Registry.Loca lMachine.OpenSu bKey(
      "System\\Curren tControlSet\\Se rvices\\" + key).GetValue( "ImagePath" ))
      == null )
      continue;

      if ( (DisplayName =Microsoft.Win3 2.Registry.Loca lMachine.OpenSu bKey(
      "System\\Curren tControlSet\\Se rvices\\" + key).GetValue( "DisplayNam e"))
      == null )
      DisplayName = "Unknow";

      sb.Append( "Service name=");
      sb.Append( key );
      sb.Append( " | ");

      sb.Append( "DisplayName=") ;
      sb.Append( DisplayName.ToS tring() );
      sb.Append( " | ");

      sb.Append( "Service image=");
      sb.Append( imagePath.ToStr ing() );

      sb.Append( Environment.New Line );
      }


      cheers,

      --
      Ignacio Machin,
      ignacio.machin AT dot.state.fl.us
      Florida Department Of Transportation


      <timburda@hotma il.com> wrote in message
      news:1114009493 .461513.85100@z 14g2000cwz.goog legroups.com...[color=blue]
      > 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.ServiceP rocess.ServiceC ontroller[] foo =
      > System.ServiceP rocess.ServiceC ontroller.GetSe rvices();
      >
      > 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
      >[/color]


      Comment

      • timburda@hotmail.com

        #4
        Re: Get path for install service

        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 ServiceControll er 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

        Comment

        • timburda@hotmail.com

          #5
          Re: Get path for install service

          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 ServiceControll er 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

          Comment

          • Willy Denoyette [MVP]

            #6
            Re: Get path for install service


            <timburda@hotma il.com> wrote in message
            news:1114009493 .461513.85100@z 14g2000cwz.goog legroups.com...[color=blue]
            > 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.ServiceP rocess.ServiceC ontroller[] foo =
            > System.ServiceP rocess.ServiceC ontroller.GetSe rvices();
            >
            > 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
            >[/color]

            Using System.Manageme nt...

            string path = ServicePath("ev ent log");
            if (path != null) // found?
            ...




            string ServicePath(str ing serviceName)
            {
            string ret = null;
            ManagementObjec tCollection Coll;
            using(Managemen tObjectSearcher Searcher = new
            ManagementObjec tSearcher("SELE CT PathName from Win32_Service
            where DisplayName =" + "\"" + serviceName + "\""))
            {
            foreach(Managem entObject service in Searcher.Get())
            {
            ret = service["PathName"].ToString();
            }
            }
            return ret;
            }

            Willy.


            Comment

            Working...