检查是否在特定的计算机上存在服务而不使用异常处理 [英] Check if a service exists on a particular machine without using exception handling
问题描述
不知道是否有更好的方式来做到这一点,所以这是该问题的原因。我可以检查是否有特定的计算机上存在的服务用下面的代码:
Don't know if there is a better way to do this, so that is the reason for the question. I can check if a service exists on a particular machine with the following code:
bool DoesServiceExist(string serviceName, string machineName)
{
ServiceController controller = null;
try
{
controller = new ServiceController(serviceName, machineName);
controller.Status;
return true;
}
catch(InvalidOperationException)
{
return false;
}
finally
{
if (controller != null)
{
controller.Dispose();
}
}
}
但这似乎像一个ineffecient溶液到我(由于异常处理)。有没有更好的方法来检查,如果一个服务存在。请注意 - 我最近切换到.NET 4.0,所以如果有人知道在4.0更好的解决方案,这将是可以接受的。
but this seems like an ineffecient solution to me (due to the exception handling). Is there a better way to check if a service exists. Note - I have recently switched to .Net 4.0 so if someone knows of a better solution in 4.0 that would be acceptable.
编辑:
这里是一个示例C#控制台应用程序来测试我的例子中的表现,以及在GetServices代码示例。在我的测试中我发现,GetServices很多在该服务不存在的情况下更多的表演,但是当服务确实存在慢一倍:
Here is a sample c# console app to test the performance of my example as well as the GetServices code sample. In my testing I found that the GetServices is much more performative in the case where the service does not exist, but is twice as slow when the service does exist:
static void Main(string[] args)
{
string serviceName = string.Empty;
string machineName = string.Empty;
var sw = new Stopwatch();
sw.Reset();
sw.Start();
for (int i = 0; i < 1000; i++)
{
ServiceExistsException(serviceName, machineName);
}
sw.Stop();
Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds.ToString());
sw.Reset();
sw.Start();
for (int i = 0; i < 1000; i++)
{
ServiceExistsGetList(serviceName, machineName);
}
sw.Stop();
Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds.ToString());
Console.WriteLine("Done");
Console.ReadLine();
}
static bool ServiceExistsException(string serviceName, string machineName)
{
ServiceController controller = null;
try
{
controller = new ServiceController(serviceName, machineName);
string name = controller.DisplayName;
return true;
}
catch (InvalidOperationException)
{
return false;
}
finally
{
if (controller != null)
{
controller.Dispose();
}
}
}
static bool ServiceExistsGetList(string serviceName, string machineName)
{
ServiceController[] services = null;
try
{
services = ServiceController.GetServices(machineName);
var service = services.FirstOrDefault(s => s.ServiceName == serviceName);
return service != null;
}
finally
{
if (services != null)
{
foreach (ServiceController controller in services)
{
controller.Dispose();
}
}
}
}
}
推荐答案
您可以使用 ServiceController.GetServices()
方法来获取所有机器上的服务,然后通过看他们是否存在一个名为你在找什么:
You can use the ServiceController.GetServices()
method to get all of the services on the machine, then look through them to see if one exists named what you are looking for:
bool DoesServiceExist(string serviceName, string machineName)
{
ServiceController[] services = ServiceController.GetServices(machineName);
var service = services.FirstOrDefault(s => s.ServiceName == serviceName);
return service != null;
}
的
的 FirstOrDefault()
扩展方法(从 System.Linq的
)将返回或者给定名称的第一个服务,或者空
如果没有匹配。
The FirstOrDefault()
extension method (from System.Linq
) will return either the first service with the given name, or a null
if there is no match.
要解决你的网速问题:
针对单个方法调用两种方法之间的差异是可以忽略不计,不管是否服务发现还是不行。它只会是一个问题,如果你调用此方法数千次&MDASH;在这种情况下获得的服务列表中一次,记得
The difference between the two approaches for a single method call is negligible, regardless of whether the service is found or not. It will only be a problem if you are calling this method thousands of times—in which case get the list of services once and remember it.
这篇关于检查是否在特定的计算机上存在服务而不使用异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!