【问题标题】:Trying to start a windows service from a windows application giving System.ComponentModel.Win32Exception: Access is denied尝试从 Windows 应用程序启动 Windows 服务,给出 System.ComponentModel.Win32Exception:访问被拒绝
【发布时间】:2010-12-10 10:18:51
【问题描述】:

我正在尝试开发一个 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();
}
}

【问题讨论】:

  • 如果你在Administrator下运行会得到同样的结果吗?
  • 我是电脑的管理员帐号
  • 不,我的意思是使用提升的权限运行它。右键,以管理员身份运行。

标签: c# system.componentmodel servicecontroller


【解决方案1】:

尝试 leppie 在他的评论中建议的内容,如果它不起作用,您需要告诉我们哪一行抛出异常 - 当您创建 ServiceController、尝试启动它或其他地方时。

顺便说一句,如果服务暂停,你不应该调用 sc.Start(),你应该调用 sc.Continue()。

另外,使用 using 结构可能比 try/finally 更好,如下所示:

private void StartService(string WinServiceName)
{
    try
    {
        using(ServiceController sc = new ServiceController(WinServiceName,"."))
        {
            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.Continue();
                }
            }
        }
    }
    catch (Exception ex)
    {
        label3.Text = ex.ToString();
        MessageBox.Show("Could not start " + WinServiceName + "Service.\n Error : " + ex.ToString(), "Error Occured", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

这样你就不需要自己调用 sc.Close() (顺便说一句,你只需要调用 Close Dispose 是多余的 - 关闭文档:断开此 ServiceController 实例与服务的连接并释放该实例的所有资源已分配。)

编辑

在资源管理器中右键单击您的 exe 文件,然后选择以管理员身份运行。在 Windows 7 中,除非您关闭了 UAC(用户访问控制),否则您不会以管理员身份运行程序,除非您明确请求/或要求您这样做。

【讨论】:

  • 嗨 mihailo,感谢您的建议。到目前为止很喜欢它们 :) 当涉及到 sc.Start(); 时,程序给出了错误;
  • 只是为了确定如何检查我是否在管理员下运行。 (我知道我在管理员帐户中,但似乎没有完全访问权限)
  • 哪个 sc.Start() ?你有两个 :) - 如果你在 Windows 7 或 Vista 上运行你的应用程序,你可能需要右键单击你的应用程序,并且应该有一个以管理员身份运行它的选项(它与 Vista 中引入的 UAC 有关) -如果您使用的是 Win XP,如果您在 Adminstrators 组中,您将以管理员身份运行所有内容。
  • 我在 Windows 7 上运行,我说的是第一个 sc.Start();
  • 安装应用程序并以管理员身份运行设置。突然它开始正常工作。 10x 为您提供帮助。还有办法避免这种情况吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多