I am using C# code to start and stop the window serves but I am getting this error.
System.ComponentModel.Win32Exception: Access is denied
My code:
public void StartService(string serviceName, int timeoutMilliseconds)
{
ServiceController service = new ServiceController(serviceName);
try
{
TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, timeout);
lblMessage.Text = "Service Started.";
}
catch (Exception ex)
{
//lblMessage.Text = "Error in Service Starting.";
lblMessage.Text = ex.ToString();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…