Check if Service Exists?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lucas Tam

    #1

    Check if Service Exists?

    Hi all,

    If I create a serviceControll er like:

    srvController = New ServiceControll er("Fubar")

    serviceControll er will be created regardless if the service exists or not.
    It will only throw an exception if I access the properties of the
    serviceControll er.

    Is there a way to check that the service actually exists before creating
    it?

    Thanks.

    --
    Lucas Tam (REMOVEnntp@rog ers.com)
    Please delete "REMOVE" from the e-mail address when replying.
    http://members.ebay.com/aboutme/coolspot18/
  • Darrell Wesley

    #2
    RE: Check if Service Exists?

    DOn't know if this the correct way or not but it's what one of our programs
    is doing to see if a Service is installed.

    '
    ' Check to see if VNC is installed
    '
    Dim scServices() = ServiceControll er.GetServices( )
    Dim i As Integer = 0

    While i < scServices.Leng th
    'MessageBox.Sho w(scServices(i) .DisplayName)
    If scServices(i).D isplayName = "VNC Server" Then
    m_bVNC = True
    End If
    i = i + 1
    End While


    If m_bVNC Then

    '
    ' Check to see if VNC is already running, If so indecate
    that VNC is running
    '
    Try

    If m_VNC Is Nothing Then
    m_VNC = New ServiceControll er("VNC Server")
    End If


    m_VNC.Refresh()

    If m_VNC.Status <> ServiceControll erStatus.Stoppe d Then
    tbVnc.ImageInde x = 7
    tbVnc.Text = "VNC - ON"
    End If

    Catch exp As Exception
    MsgBox(m_VNC.Se rviceName & " is in state:" &
    m_VNC.Status.To String() & vbNewLine & _
    "Could not start service")

    Finally
    m_VNC.Dispose()
    m_VNC = Nothing
    End Try

    Else
    tbVnc.Enabled = False

    End If


    "Lucas Tam" wrote:
    [color=blue]
    > Hi all,
    >
    > If I create a serviceControll er like:
    >
    > srvController = New ServiceControll er("Fubar")
    >
    > serviceControll er will be created regardless if the service exists or not.
    > It will only throw an exception if I access the properties of the
    > serviceControll er.
    >
    > Is there a way to check that the service actually exists before creating
    > it?
    >
    > Thanks.
    >
    > --
    > Lucas Tam (REMOVEnntp@rog ers.com)
    > Please delete "REMOVE" from the e-mail address when replying.
    > http://members.ebay.com/aboutme/coolspot18/
    >[/color]

    Comment

    • Lucas Tam

      #3
      RE: Check if Service Exists?

      "=?Utf-8?B?RGFycmVsbCB XZXNsZXk=?="
      <DarrellWesley@ discussions.mic rosoft.com> wrote in
      news:5FF845E6-2CF6-474B-938B-B0288CB20A7F@mi crosoft.com:
      [color=blue]
      > DOn't know if this the correct way or not but it's what one of our
      > programs is doing to see if a Service is installed.
      >
      > '
      > ' Check to see if VNC is installed
      > '
      > Dim scServices() = ServiceControll er.GetServices( )
      > Dim i As Integer = 0
      >
      > While i < scServices.Leng th
      > 'MessageBox.Sho w(scServices(i) .DisplayName)
      > If scServices(i).D isplayName = "VNC Server" Then
      > m_bVNC = True
      > End If
      > i = i + 1
      > End While[/color]

      Thank you - I was hoping to avoid looping through all my services. I was
      sort of expecting that .NET would throw an error if it tried to create
      an non-existing service.

      i.e. New ServiceControll er("NoSuchServi ce")

      But it doesn't.

      In anycase, the GetServices method way will work just fine for me.

      Thank you : )


      --
      Lucas Tam (REMOVEnntp@rog ers.com)
      Please delete "REMOVE" from the e-mail address when replying.
      http://members.ebay.com/aboutme/coolspot18/

      Comment

      • Darrell Wesley

        #4
        RE: Check if Service Exists?

        DOn't know if this the correct way or not but it's what one of our programs
        is doing to see if a Service is installed.

        '
        ' Check to see if VNC is installed
        '
        Dim scServices() = ServiceControll er.GetServices( )
        Dim i As Integer = 0

        While i < scServices.Leng th
        'MessageBox.Sho w(scServices(i) .DisplayName)
        If scServices(i).D isplayName = "VNC Server" Then
        m_bVNC = True
        End If
        i = i + 1
        End While


        If m_bVNC Then

        '
        ' Check to see if VNC is already running, If so indecate
        that VNC is running
        '
        Try

        If m_VNC Is Nothing Then
        m_VNC = New ServiceControll er("VNC Server")
        End If


        m_VNC.Refresh()

        If m_VNC.Status <> ServiceControll erStatus.Stoppe d Then
        tbVnc.ImageInde x = 7
        tbVnc.Text = "VNC - ON"
        End If

        Catch exp As Exception
        MsgBox(m_VNC.Se rviceName & " is in state:" &
        m_VNC.Status.To String() & vbNewLine & _
        "Could not start service")

        Finally
        m_VNC.Dispose()
        m_VNC = Nothing
        End Try

        Else
        tbVnc.Enabled = False

        End If


        "Lucas Tam" wrote:
        [color=blue]
        > Hi all,
        >
        > If I create a serviceControll er like:
        >
        > srvController = New ServiceControll er("Fubar")
        >
        > serviceControll er will be created regardless if the service exists or not.
        > It will only throw an exception if I access the properties of the
        > serviceControll er.
        >
        > Is there a way to check that the service actually exists before creating
        > it?
        >
        > Thanks.
        >
        > --
        > Lucas Tam (REMOVEnntp@rog ers.com)
        > Please delete "REMOVE" from the e-mail address when replying.
        > http://members.ebay.com/aboutme/coolspot18/
        >[/color]

        Comment

        • Lucas Tam

          #5
          RE: Check if Service Exists?

          "=?Utf-8?B?RGFycmVsbCB XZXNsZXk=?="
          <DarrellWesley@ discussions.mic rosoft.com> wrote in
          news:5FF845E6-2CF6-474B-938B-B0288CB20A7F@mi crosoft.com:
          [color=blue]
          > DOn't know if this the correct way or not but it's what one of our
          > programs is doing to see if a Service is installed.
          >
          > '
          > ' Check to see if VNC is installed
          > '
          > Dim scServices() = ServiceControll er.GetServices( )
          > Dim i As Integer = 0
          >
          > While i < scServices.Leng th
          > 'MessageBox.Sho w(scServices(i) .DisplayName)
          > If scServices(i).D isplayName = "VNC Server" Then
          > m_bVNC = True
          > End If
          > i = i + 1
          > End While[/color]

          Thank you - I was hoping to avoid looping through all my services. I was
          sort of expecting that .NET would throw an error if it tried to create
          an non-existing service.

          i.e. New ServiceControll er("NoSuchServi ce")

          But it doesn't.

          In anycase, the GetServices method way will work just fine for me.

          Thank you : )


          --
          Lucas Tam (REMOVEnntp@rog ers.com)
          Please delete "REMOVE" from the e-mail address when replying.
          http://members.ebay.com/aboutme/coolspot18/

          Comment

          Working...