gpt4 book ai didi

c# - 获取已安装服务的版本信息?

转载 作者:太空狗 更新时间:2023-10-29 21:19:51 25 4
gpt4 key购买 nike

我想以编程方式检查是否安装了最新版本的 Windows 服务。我有:

var ctl = ServiceController.GetServices().Where(s => s.ServiceName == "MyService").FirstOrDefault();
if (ctl != null) {
// now what?
}

我在 ServiceController 界面上没有看到任何可以告诉我版本号的信息。我该怎么做?

最佳答案

恐怕除了从注册表中获取可执行路径之外别无他法,因为 ServiceController 不提供该信息。

这是我之前创建的示例:

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)类获取版本即可。

关于c# - 获取已安装服务的版本信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4555350/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com