I am trying to develop a windows application to start/stop and monitor status of two particular services.
The problem is I am getting
System.ComponentModel.Win32Exception: Access is denied
Note that both services are local system
The following is my code
private void StartService(string WinServiceName)
{
ServiceController sc = new ServiceController(WinServiceName,".");
try
{
if (sc.ServiceName.Equals(WinServiceName))
{
//check if service stopped
if (sc.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Stopped))
{
sc.Start();
}
else if (sc.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Paused))
{
sc.Start();
}
}
}
catch (Exception ex)
{
label3.Text = ex.ToString();
MessageBox.Show("Could not start " + WinServiceName + "Service.\n Error : " + ex.ToString(), "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
sc.Close();
sc.Dispose();
// CheckStatus();
}
}