2 つの特定のサービスの状態を開始/停止および監視する Windows アプリケーションを開発しようとしています。
問題は私が得ていることです
System.ComponentModel.Win32Exception: アクセスが拒否されました
両方のサービスがローカル システムであることに注意してください
以下は私のコードです
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();
}
}