using Microsoft.Win32; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text.RegularExpressions; namespace WelsonJS.Launcher { public class ExecutablesCollector { private List executables = new List(); public ExecutablesCollector() { executables.AddRange(GetInstalledSoftwareExecutables()); executables.AddRange(GetExecutablesFromPath()); } public List GetExecutables() { return executables; } private List GetInstalledSoftwareExecutables() { List executables = new List(); string registryKey = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; using (RegistryKey key = Registry.LocalMachine.OpenSubKey(registryKey)) { if (key != null) { foreach (string subKeyName in key.GetSubKeyNames()) { using (RegistryKey subKey = key.OpenSubKey(subKeyName)) { string installLocation = subKey?.GetValue("InstallLocation") as string; string uninstallString = subKey?.GetValue("UninstallString") as string; List executablePaths = FindExecutables(installLocation, uninstallString); executables.AddRange(executablePaths); } } } } return executables; } private List FindExecutables(string installLocation, string uninstallString) { List executables = new List(); if (!string.IsNullOrEmpty(installLocation) && Directory.Exists(installLocation)) { try { List executableFiles = Directory.GetFiles(installLocation, "*.exe", SearchOption.AllDirectories) .OrderByDescending(f => new FileInfo(f).Length) .ToList(); executables.AddRange(executableFiles); } catch (Exception ex) { Debug.WriteLine($"Error enumerating executables in '{installLocation}': {ex}"); } } if (!string.IsNullOrEmpty(uninstallString)) { if (TryParseExecutablePath(uninstallString, out string executablePath)) { executables.Add(executablePath); } } return executables; } private static bool TryParseExecutablePath(string s, out string path) { Match match = Regex.Match(s, @"(?<=""|^)([a-zA-Z]:\\[^""]+\.exe)", RegexOptions.IgnoreCase); if (match.Success) { path = match.Value; return true; } path = null; return false; } private List GetExecutablesFromPath() { List executables = new List(); string pathEnv = Environment.GetEnvironmentVariable("PATH"); if (!string.IsNullOrEmpty(pathEnv)) { foreach (string path in pathEnv.Split(';')) { if (Directory.Exists(path)) { try { executables.AddRange(Directory.GetFiles(path, "*.exe", SearchOption.TopDirectoryOnly)); } catch (Exception ex) { Debug.WriteLine($"Error enumerating executables in '{path}': {ex}"); } } } } return executables; } } }