在应用程序启动时禁用 Windows 服务
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3052649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable Windows service at application launch
提问by SiberianGuy
Because of VLC conflictI have to turn off Windows Advanced Text Services at my application launch. Is there any special API for that? Will it work for user with default rights?
由于VLC 冲突,我必须在应用程序启动时关闭 Windows 高级文本服务。有什么特殊的API吗?它适用于具有默认权限的用户吗?
采纳答案by Paulo Henrique
ServiceController _ServiceController = new ServiceController([NameService]);
if (!_ServiceController.ServiceHandle.IsInvalid)
{
_ServiceController.Stop();
_ServiceController.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromMilliseconds(uConstante.CtTempoEsperaRespostaServico));
}
回答by xr280xr
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");
回答by Fredrik M?rk
You can use the ServiceController
class for this. There are code samples in the linked documentation page.
您可以ServiceController
为此使用该类。链接的文档页面中有代码示例。
回答by pixar
just execute "net stop service-name" to stop a service or "net start service-name" to start a service. type "net start" in the console (cmd.exe) in order to list all services.
只需执行“net stop service-name”来停止服务或“net start service-name”来启动服务。在控制台 (cmd.exe) 中键入“net start”以列出所有服务。
You need admin privileges in order to enable/disable services.
您需要管理员权限才能启用/禁用服务。
回答by Pavel Radzivilovsky
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