Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 安装后自动启动服务_C#_.net_Windows Services - Fatal编程技术网

C# 安装后自动启动服务

C# 安装后自动启动服务,c#,.net,windows-services,C#,.net,Windows Services,这是一个使用VS2015和.NET Framework 4.5的Windows服务项目 我正在尝试通过生成后操作安装我的服务,然后使用ServiceController.start()自动启动它。以下是我尝试启动服务的代码: private void ProjectInstaller_Committed(object sender, InstallEventArgs e) { using (var sw = new System.IO.StreamWriter(Console.OpenSta

这是一个使用VS2015和.NET Framework 4.5的Windows服务项目

我正在尝试通过生成后操作安装我的服务,然后使用
ServiceController.start()
自动启动它。以下是我尝试启动服务的代码:

private void ProjectInstaller_Committed(object sender, InstallEventArgs e)
{
  using (var sw = new System.IO.StreamWriter(Console.OpenStandardOutput()))
  {
    using (ServiceController sc = new ServiceController(serviceInstaller1.ServiceName))
    {
      try
      {
        sw.Write("Starting service...");
        sc.Start();
        sc.WaitForStatus(ServiceControllerStatus.Running);
        sw.Write("Service status is now set to {0}.", sc.Status.ToString());
      }
      catch (InvalidOperationException)
      {
        sw.Write("Could not start the service.");
      }
    }
  }
}

该服务安装得很好,但我的
ServiceController.WaitForStatus()
调用似乎一直在等待。我试着从
Committed
AfterInstall
事件中调用它。

最终找到了答案。我的
Start()。错误消息类似于:

Your process does not have access rights to this namespace
在谷歌上搜索错误消息时,我发现接受的答案对我起了作用。希望这对以后的人有所帮助