c# – 获取已安装服务的版本信息?
发布时间:2020-12-15 08:38:49 所属栏目:百科 来源:网络整理
导读:我想以编程方式检查是否安装了最新版本的 Windows服务.我有: var ctl = ServiceController.GetServices().Where(s = s.ServiceName == "MyService").FirstOrDefault();if (ctl != null) { // now what?} 我在ServiceController接口上看不到任何会告诉我版本
我想以编程方式检查是否安装了最新版本的
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 = @"SYSTEMCurrentControlSetServices" + 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)类来获取版本. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |