如果将相同的exe设置为多个服务,则为当前服务的名称 [英] Name of current service in case the same exe was setup as multiple services
问题描述
嗨朋友们,
如果将同一个exe设置为多个服务,当前服务的名称
有没有办法从C#获取当前服务在安装时注册的服务名称。
例如,我两次注册相同的服务:
sc.exe创建ServiceName1 binPath = D:\ myservice.exe
sc.exe创建ServiceName2 binPath = D:\ myservice.exe
我已经安装了这两项服务。我想杀死ServiceName1的sc.exe,如何检查sc.exe是否属于ServiceName1。
请帮帮我。
< br $>
谢谢,
-RG
Hi friends,
Name of current service in case the same exe was setup as multiple services
Is there a way to get the service name from C# that the current service was registered with when it was installed.
For instance, I register the same service twice:
sc.exe create ServiceName1 binPath= D:\myservice.exe
sc.exe create ServiceName2 binPath= D:\myservice.exe
I have installed both services. I want to kill sc.exe of ServiceName1, how to check whether sc.exe belongs to ServiceName1.
Please help me.
Thanks,
-RG
推荐答案
你想用的System.ServiceProcess中的ServiceController类。一个简单的例子:
You'll want to use the ServiceController class from System.ServiceProcess. A simple example:
using System;
using System.ServiceProcess;
public class ServiceUtil
{
public static void StopService(string pName)
{
try
{
ServiceController sc = new (pName, Environment.MachineName);
if (sc.Status == ServiceControllerStatus.Running) sc.Stop();
}
catch (ArgumentException)
{
// service does not exist
}
}
}
这篇关于如果将相同的exe设置为多个服务,则为当前服务的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!