c# 通过注册表获取系统服务安装路径

发布时间 2023-06-14 18:01:30作者: Hey,Coder!
string key = @"SYSTEM\CurrentControlSet\Services\";
            var services = Registry.LocalMachine.OpenSubKey(key);
            if (services == null)
            {
                return;
            }
            var serviceNameList = services.GetSubKeyNames().ToList();
            if (serviceNameList != null && serviceNameList.Count > 0)
            {
                //serviceNameList= serviceNameList.Where(f=>f.Contains("K3CloudClienter")).ToList();
                foreach (var serviceName in serviceNameList)
                {
                    var serviceKey = Registry.LocalMachine.OpenSubKey($"{key}\\{serviceName}");
                    if (serviceKey != null)
                    {
                        var configValue = serviceKey.GetValue("ImagePath");
                        if (configValue == null)
                        {
                            continue;
                        }
                        System.Console.WriteLine(configValue.ToString());
                    }
                }
            }