Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Exception: Cannot open Servicexxx service on computer 'COMPUTER_NAME'

Exception: Cannot open Servicexxx service on computer 'COMPUTER_NAME'

Scheduled Pinned Locked Moved C#
tutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NarVish
    wrote on last edited by
    #1

    I used below code to start service.

    var sc = new System.ServiceProcess.ServiceController("ELSRegressionService");
    if (sc.Status.ToString().Equals("Stopped"))
    sc.Start();

    Its working fine, when logged in with Administrator credentials. If I login with other user credentials, it throws exception, "Cannot open Servicexxx service on computer 'COMPUTER_NAME'" I would like to ask for credentials before the user tries to start service. So please guide me to check for credentials to start windows service programmatically. Thanks in advance

    N D 2 Replies Last reply
    0
    • N NarVish

      I used below code to start service.

      var sc = new System.ServiceProcess.ServiceController("ELSRegressionService");
      if (sc.Status.ToString().Equals("Stopped"))
      sc.Start();

      Its working fine, when logged in with Administrator credentials. If I login with other user credentials, it throws exception, "Cannot open Servicexxx service on computer 'COMPUTER_NAME'" I would like to ask for credentials before the user tries to start service. So please guide me to check for credentials to start windows service programmatically. Thanks in advance

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      NarVish wrote:

      if (sc.Status.ToString().Equals("Stopped"))

      Correct way to do the check is

      if (sc.Status == ServiceControllerStatus.Stopped) {
      sc.Start();
      }

      NarVish wrote:

      I would like to ask for credentials before the user tries to start service.

      Read about Application manifest and UAC[^]. If you need administrator privilege only for starting the service, you could delay request for elevation till you need it. This can be done by starting a new process and setting ProcessStartInfo.Verb[^] to runas. This new process can execute the code to start/stop the service. You need the new process because you can't change elevation levels of the current process while it is running.

      Best wishes, Navaneeth

      1 Reply Last reply
      0
      • N NarVish

        I used below code to start service.

        var sc = new System.ServiceProcess.ServiceController("ELSRegressionService");
        if (sc.Status.ToString().Equals("Stopped"))
        sc.Start();

        Its working fine, when logged in with Administrator credentials. If I login with other user credentials, it throws exception, "Cannot open Servicexxx service on computer 'COMPUTER_NAME'" I would like to ask for credentials before the user tries to start service. So please guide me to check for credentials to start windows service programmatically. Thanks in advance

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        If you're going to be asking for a username and password, what's the problem?? It's two labels and two textboxes on a small form and showing it!

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups