获取已安装服务的版本信息? [英] Get the version information of an installed service?
本文介绍了获取已安装服务的版本信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想以编程方式检查是否安装了最新版本的 Windows 服务.我有:
I want to check programmatically that the latest version of my Windows Service is installed. I have:
var ctl = ServiceController.GetServices().Where(s => s.ServiceName == "MyService").FirstOrDefault();
if (ctl != null) {
// now what?
}
我在 ServiceController
界面上看不到任何会告诉我版本号的内容.我该怎么做?
I don't see anything on the ServiceController
interface that will tell me the version number. How do I do it?
推荐答案
恐怕除了从注册表获取可执行路径之外别无他法,因为 ServiceController
不提供该信息.
I am afraid there is no way other than getting the executable path from the registry as ServiceController
does not provide that information.
这是我之前创建的示例:
Here is a sample I had created before:
private static string GetExecutablePathForService(string serviceName, RegistryView registryView, bool throwErrorIfNonExisting)
{
string registryPath = @"SYSTEM\CurrentControlSet\Services\" + serviceName;
RegistryKey key = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, registryView).OpenSubKey(registryPath);
if(key==null)
{
if (throwErrorIfNonExisting)
throw new ArgumentException("Non-existent service: " + serviceName, "serviceName");
else
return null;
}
string value = key.GetValue("ImagePath").ToString();
key.Close();
if(value.StartsWith("\""))
{
value = Regex.Match(value, "\"([^\"]+)\"").Groups[1].Value;
}
return Environment.ExpandEnvironmentVariables(value);
}
获取exe路径后,直接使用FileVersionInfo.GetVersionInfo(exePath)
类获取版本即可.
After getting the exe path, just use FileVersionInfo.GetVersionInfo(exePath)
class to get the version.
这篇关于获取已安装服务的版本信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文