简体   繁体   English

在应用程序启动时禁用Windows服务

[英]Disable Windows service at application launch

Because of VLC conflict I have to turn off Windows Advanced Text Services at my application launch. 由于VLC冲突,我必须在我的应用程序启动时关闭Windows高级文本服务。 Is there any special API for that? 那有什么特殊的API吗? Will it work for user with default rights? 它是否适用于具有默认权限的用户?

The question is titled "Disable Windows service..." but the answers all tell how to stop a service. 问题标题为“禁用Windows服务...”,但答案都告诉我们如何停止服务。

Most of what you will find on Google is that you can update the registry to disable a service using something like this: 您在Google上可以找到的大多数内容是您可以使用以下内容更新注册表以禁用服务:

RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\[YourServiceName]", true);
key.SetValue("Start", 4);

I haven't tried that method but it appears as though it will work. 我没有尝试过这种方法,但似乎它会起作用。 I also came up with a different method that uses sc.exe to disable the service: 我还想出了一个使用sc.exe来禁用该服务的不同方法:

Process sc = Process.Start("sc.exe", "config [YourServiceName] start= disabled");
ServiceController _ServiceController = new ServiceController([NameService]);
if (!_ServiceController.ServiceHandle.IsInvalid) 
{
     _ServiceController.Stop();
     _ServiceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMilliseconds(uConstante.CtTempoEsperaRespostaServico));
}

You can use the ServiceController class for this. 您可以使用ServiceController类。 There are code samples in the linked documentation page. 链接的文档页面中有代码示例。

You can use WMI for that. 您可以使用WMI。

Look here, for example: http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=114 看这里,例如: http//www.csharpfriends.com/Articles/getArticle.aspx?articleID = 114

just execute "net stop service-name" to stop a service or "net start service-name" to start a service. 只需执行“net stop service-name”来停止服务或“net start service-name”来启动服务。 type "net start" in the console (cmd.exe) in order to list all services. 在控制台(cmd.exe)中键入“net start”以列出所有服务。

You need admin privileges in order to enable/disable services. 您需要管理员权限才能启用/禁用服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM