Question cannot open service "servicename" on computer '.'.

Nucleus

Active member
Joined
May 24, 2005
Messages
30
Programming Experience
Beginner
For some reason, when using the code below, when I click any of the 2 buttons to stop or start the spooler service, I get the error attached. I've googled it, but I could not find the reason why. Any ideas?

VB.NET:
Imports System.ServiceProcess
Public Class Form1
    Dim Spooler As New ServiceController("Spooler")
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If Spooler.Status.Equals(ServiceControllerStatus.Running) Then Label1.Text = "Running" : PictureBox1.BackColor = Color.Green : Button1.Enabled = False : Button2.Enabled = True
        If Spooler.Status.Equals(ServiceControllerStatus.Stopped) Then Label1.Text = "Stopped" : PictureBox1.BackColor = Color.Red : Button1.Enabled = True : Button2.Enabled = False
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Spooler.Start()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Spooler.Stop()
    End Sub
End Class
 

Attachments

  • 1.png
    1.png
    9.5 KB · Views: 28
Most likely you can only do that running as administrator in elevated mode.
 
You are using W7 or W8 and can't really turn off UAC, you can set it to never notify and elevate without prompt. You still need to make the app request elevation, even when run with administrator account. In Application page in project properties there is a 'View Windows Settings' button that will bring up the app.manifest, and in there you can set requestedExecutionLevel level="requireAdministrator" to force this mode when application starts.
 
Back
Top