以编程方式启动服务

发布于 2024-12-11 14:11:05 字数 866 浏览 0 评论 0原文

我正在努力从另一个应用程序启动服务。

好吧,首先,这是我的出发点: 我将一个常用的服务包装到我自己的类中,该类继承自 ServiceBase。 此外,我得到了一个 ProjectInstaller 实例,它继承自 System.Configuration.Install.Installer 并负责以正确的方式在系统上安装我的服务。

它有以下参数:

serviceInstaller1.ServiceName = "NameOfService";
serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Manual;
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;

此 ProjectInstaller 输出是通过通常的 C# .NET 安装项目安装的。

该服务显示在系统控制内的 GUI 服务列表中 ->管理->服务。但我想知道的是,如果我使用 cmd.exe 并输入“net start”,该服务不会出现在服务列表中。

这就是我到目前为止所拥有的。 现在我想从另一个应用程序启动我的服务。该应用程序继承自“

ServiceController

如果我现在想调用该

Start()

方法,我收到一条错误,告诉我无法在本地计算机上打开该服务”。

我使用与上述相同的服务名称。我对此感到困惑,因为我认为该服务是通过我的安装程序正确安装的。 或者我的应用程序(以及 cmd.exe)是否没有足够的权限来访问该服务?

I'm struggling with starting a service from another application.

Ok, first, that's my starting point:
I got a usual service wrapped into my own class which inherits from ServiceBase.
Furthermore I got a ProjectInstaller instance, which inherits from System.Configuration.Install.Installer and takes care of installing my service on the system in the right way.

it's got these parameters:

serviceInstaller1.ServiceName = "NameOfService";
serviceInstaller1.StartType = System.ServiceProcess.ServiceStartMode.Manual;
serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;

This ProjectInstaller output is installed through a usual C# .NET installation project.

The service is shown in the GUI list of services within system control -> administration -> services. But what I'm wondering about is that the service does not appear in the list of services, if I use the cmd.exe and type "net start".

That's waht I have so far.
Now I want to start my service from another application. This application inherits from

ServiceController

If I want to call now the

Start()

method, I get an error telling me that the service could not be opened of the local machine.

I use the same Service Name as stated above. T'm confused about that, because I thought the service is installed correctly through my installer.
Or does my application (and the cmd.exe too) does not have enough rights to access the service?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

转身泪倾城 2024-12-18 14:11:05

我在 stackoverflow 上找到了这篇文章。
我认为发布在那里的答案也可以应用在这里。

请参阅下面的链接
Windows 7 中的 ServiceController 权限

I found this article on stackoverflow.
I think this answer posted there can be applied here too.

see the link below
ServiceController permissions in Windows 7

楠木可依 2024-12-18 14:11:05

我以前遇到过这个问题,对我来说也是基于权限的。我希望该错误消息更清楚一些。

我建议您查看这篇文章:C#.NET:获取管理员权限? - 答案中似乎有一个关于升级到管理权限的很好的教程。

I've run into this issue before, and it was permissions-based for me, too. I wish that error message was a little more clear.

I'd recommend taking a look at this SO article: C#.NET: Acquire administrator rights? - it looks like there's a good tutorial about upgrading to administrative rights provided in the answers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文