/src/Kweh.Core/Win32/RegistryUtil.cs
C# | 320 lines | 280 code | 39 blank | 1 comment | 52 complexity | b47d73449bc0646243c2ccd77df77ee3 MD5 | raw file
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading.Tasks;
- namespace BadMishka.Kweh.Win32
- {
- public static class RegistryUtil
- {
- public static RegistryKey FindUninstallKey(string displayName, Func<string, string, bool> comparison = null, string sid = null)
- {
- if (comparison == null)
- comparison = (left, right) =>
- {
- if (string.IsNullOrWhiteSpace(right))
- return false;
- return left.StartsWith(right);
- };
- var list = new List<string>()
- {
- @"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
- @"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
- @"HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
- @"HKCU:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
- $"HKU:\\{sid}\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
- $"HKU:\\{sid}\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
- };
- var noSid = string.IsNullOrWhiteSpace(sid);
- foreach(var subKeyName in list)
- {
- if (subKeyName.StartsWith("HKU") && noSid)
- continue;
- var subKey = OpenSubKey(subKeyName);
- if (subKey == null)
- continue;
- var subKeyNames = subKey.GetSubKeyNames();
- foreach(var guidSubKeyName in subKeyNames)
- {
- var guidSubKey = subKey.OpenSubKey(guidSubKeyName);
- var productDisplayName = guidSubKey.GetValue("DisplayName") as string;
- if (productDisplayName == null)
- continue;
- if (comparison(productDisplayName, displayName))
- return guidSubKey;
- }
- }
- return null;
- }
- public static RegistryKey FindUninstallKey(Guid productId, Func<string, string, bool> comparison = null, string sid = null)
- {
- if (comparison == null)
- comparison = (left, right) =>
- {
- if (string.IsNullOrWhiteSpace(right))
- return false;
- return left.StartsWith(right);
- };
- var list = new List<string>()
- {
- @"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
- @"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
- @"HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
- @"HKCU:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
- $"HKU:\\{sid}\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
- $"HKU:\\{sid}\\SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall",
- };
- var noSid = string.IsNullOrWhiteSpace(sid);
- foreach (var subKeyName in list)
- {
- if (subKeyName.StartsWith("HKU") && noSid)
- continue;
- var subKey = OpenSubKey(subKeyName + "\\" + productId.ToString());
- if (subKey != null)
- return subKey;
- }
- return null;
- }
- public static RegistryKey OpenSubKey(string registryPath)
- {
- if (string.IsNullOrWhiteSpace(registryPath))
- throw new ArgumentNullException(nameof(registryPath));
- if(!registryPath.Contains(":"))
- throw new ArgumentException("registryPath is missing registry drive", nameof(registryPath));
- var delimiterIndex = registryPath.IndexOf(":");
- var drive = registryPath.Substring(0, delimiterIndex);
- var subKey = registryPath.Substring(delimiterIndex + 2);
- Console.WriteLine(drive);
- Console.WriteLine(subKey);
- switch (drive)
- {
- case "HKU":
- return Registry.Users.OpenSubKey(subKey);
- case "HKCU":
- return Registry.CurrentUser.OpenSubKey(subKey);
- case "HKLM":
- return Registry.LocalMachine.OpenSubKey(subKey);
- default:
- throw new NotSupportedException($"Unknown registry drive {drive}");
- }
- }
- public static string FindEnvironmentVariableForUser(string variableName, string sid = null)
- {
- var normalizedName = variableName.ToLowerInvariant();
- RegistryKey sidKey = null;
- RegistryKey subKey = null;
- var mapped = new Dictionary<string, string>()
- {
- { "localappdata", "Local AppData"},
- { "local appdata", "Local AppData"},
- { "documents", "Personal" }
- };
- if (sid == null)
- {
- switch (normalizedName)
- {
- case "tmp":
- return Environment.GetEnvironmentVariable("TEMP");
- case "local appdata":
- return Environment.GetEnvironmentVariable("LOCALAPPDATA");
- case "onedrive":
- subKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
- if (subKey == null)
- return null;
- subKey = subKey.OpenSubKey("Personal");
- if (subKey == null)
- return null;
- return subKey.GetValue("UserFolder") as string;
- case "onedrivebusiness":
- subKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
- if (subKey == null)
- return null;
- subKey = subKey.OpenSubKey("Business1");
- if (subKey == null)
- return null;
- return subKey.GetValue("UserFolder") as string;
- case "documents":
- case "my music":
- case "my pictures":
- case "my video":
- case "personal":
- case "cache":
- case "cookies":
- case "fonts":
- case "history":
- case "favorites":
- case "desktop":
- case "programs":
- var localName = variableName;
- if (mapped.ContainsKey(normalizedName))
- localName = mapped[normalizedName];
- subKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders");
- if (subKey == null)
- return null;
- return subKey.GetValue(localName) as string;
- }
- return Environment.GetEnvironmentVariable(variableName);
- }
- switch (normalizedName)
- {
- case "userprofile":
- subKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" + sid);
- if (subKey == null)
- return null;
- return subKey.GetValue("ProfileImagePath") as string;
- case "onedrive":
- sidKey = Registry.Users.OpenSubKey(sid);
- if (sidKey == null)
- return null;
- subKey = sidKey.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
- if (subKey == null)
- return null;
- subKey = subKey.OpenSubKey("Personal");
- if (subKey == null)
- return null;
- return subKey.GetValue("UserFolder") as string;
- case "onedrivebusiness":
- sidKey = Registry.Users.OpenSubKey(sid);
- if (sidKey == null)
- return null;
- subKey = sidKey.OpenSubKey(@"SOFTWARE\Microsoft\OneDrive\Accounts");
- if (subKey == null)
- return null;
- subKey = subKey.OpenSubKey("Business1");
- if (subKey == null)
- return null;
- return subKey.GetValue("UserFolder") as string;
- case "temp":
- case "tmp":
- case "local appdata":
- case "localappdata":
- case "appdata":
- case "documents":
- case "personal":
- case "cache":
- case "cookies":
- case "my music":
- case "my pictures":
- case "my video":
- case "favorites":
- case "fonts":
- case "history":
- case "desktop":
- case "programs":
- var localName = variableName;
- if (mapped.ContainsKey(normalizedName))
- localName = mapped[normalizedName];
- sidKey = Registry.Users.OpenSubKey(sid);
- if (sidKey == null)
- return null;
- subKey = sidKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders");
- if (subKey == null)
- return null;
- var value = subKey.GetValue(localName) as string;
- if (value != null && normalizedName == "temp" || normalizedName == "tmp")
- return Path.Combine(value , "Temp");
- return value;
- default:
- subKey = Registry.Users.OpenSubKey(sid);
-
- if (subKey != null)
- {
- subKey = subKey.OpenSubKey(@"Environment");
- if (subKey != null)
- {
- var keys = subKey.GetSubKeyNames();
- foreach (var key in keys)
- {
- if (normalizedName == key.ToLowerInvariant())
- {
- var path = subKey.GetValue(key) as string;
- if (path.Contains("%USERPROFILE%"))
- {
- subKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" + sid);
- if (subKey != null)
- {
- var userProfile = subKey.GetValue("ProfileImagePath") as string;
- path = path.Replace("%USERPROFILE%", userProfile ?? "");
- }
- }
- if (path.Contains("%"))
- {
- //TODO handle other values;
- }
- return path;
- }
- }
- }
- }
- var globalVariables = Environment.GetEnvironmentVariables();
- foreach (string key in globalVariables.Keys)
- {
- if (key.ToLowerInvariant() == normalizedName)
- {
- return Environment.GetEnvironmentVariable(key);
- }
- }
- return null;
- }
- }
- }
- }