static void Main(string[] args) { if (File.Exists(programsPath) && File.Exists(publicKeyPath) && File.Exists(privateKeyPath)) { rsaCryption = new RSACryption(AppLib.ReadFromFile(publicKeyPath), AppLib.ReadFromFile(privateKeyPath)); ReadProgramsList(programsPath); List <InstalledProgram> nowInstalledPrograms = InstalledProgram.GetInstalledPrograms(); CompareAndWriteLog(nowInstalledPrograms, installedPrograms); } //第一次 or 遺失任一檔案 else { rsaCryption = new RSACryption(); AppLib.WriteToFile(rsaCryption.PublicKey, publicKeyPath); AppLib.WriteToFile(rsaCryption.PrivateKey, privateKeyPath); installedPrograms = InstalledProgram.GetInstalledPrograms(); WriteProgramsList(installedPrograms, programsPath); } Console.WriteLine("目前記錄取得{0}筆已安裝軟體", installedPrograms.Count); foreach (var item in installedPrograms) { Console.WriteLine(item.DisplayName); } Console.WriteLine("---------------------------------------"); WMIReceiveEvent(); Console.ReadKey(); }
public InstalledListDataLog(InstalledProgram installedProgram, string eventType) { Name = installedProgram.DisplayName; Version = installedProgram.Version; Size = installedProgram.EstimatedSize; InstallDate = installedProgram.InstallDate; EventType = eventType; }
public void TestGetInstalledProgramByName_Chrome() { string name = "Google Chrome"; InstalledProgram product = InstalledProgramsHelper.GetInstalledProgramByName(name); bool actual = product.DisplayName.Contains(name); Assert.AreEqual(true, actual); }
public void TestGetInstalledProgramByName_Regex_VisualStudio() { Regex name = new Regex("[M|m]icrosoft [V|v]isual [S|s]tudio"); InstalledProgram product = InstalledProgramsHelper.GetInstalledProgramByName(name); bool actual = product != null; Assert.AreEqual(true, actual); }
private static void HandleEvent(object sender, EventArrivedEventArgs e) { List <InstalledProgram> installedProgramsNew = InstalledProgram.GetInstalledPrograms(); CompareAndWriteLog(installedProgramsNew, installedPrograms); //var properties = e.NewEvent.Properties; //foreach (var p in properties) //{ // Console.WriteLine("{0} -- {1}", p.Name, p.Value); //} //Console.WriteLine("---------------------------------------"); }
static void GetShunraProductDetails(InstalledProgram p) { string currentVersion = RegistryWrapper.GetRegKey32(RegHive.LocalMachine, @"SOFTWARE\Shunra\Bootstrapper", "CurrentVersion"); string buildVersion = RegistryWrapper.GetRegKey32(RegHive.LocalMachine, @"SOFTWARE\Shunra\Bootstrapper", "BuildVersion"); string installedPath = RegistryWrapper.GetRegKey32(RegHive.LocalMachine, @"SOFTWARE\Shunra\Bootstrapper", "InstalledPath"); productDetails = new StringBuilder(); productDetails.Append(Html.B(p.DisplayName) + Helper.ConvertInstallDate(p.InstallDate) + Html.br); productDetails.Append("Version: " + currentVersion + Html.br); productDetails.Append("Build: " + buildVersion + Html.br); productDetails.Append("Location: " + installedPath + Html.br + Html.br); productDetails.Append("Services: " + Html.br); productDetails.Append("Shunra WatchDog Service: " + PCServicesCollectorHelper.GetServiceInfo("ShunraWatchDogService") + Html.br); productDetails.Append("Shunra Performance Counters Service: " + PCServicesCollectorHelper.GetServiceInfo("ShunraPerformanceCountersService") + Html.br); }
private void GetProgramsFromRegistryKey(List <InstalledProgram> installedPrograms, RegistryKey registryKey) { using (registryKey) { foreach (string subkeyName in registryKey.GetSubKeyNames()) { using (RegistryKey subkey = registryKey.OpenSubKey(subkeyName)) { try { InstalledProgram program = new InstalledProgram(); string applicationName = subkey.GetValue(ConstString.REGISTRY_DISPLAY_NAME)?.ToString() ?? string.Empty; if (!string.IsNullOrEmpty(applicationName) && !installedPrograms.Cast <InstalledProgram>().ToList().Any(x => x.Name == applicationName)) { program.Name = applicationName; string installDateAsString = subkey.GetValue(ConstString.REGISTRY_INSTALL_DATE)?.ToString(); if (installDateAsString != null) { program.InstallDate = DateTimeHelper.ConvertRegistryDateStringToCorrectDateTimeFormat(installDateAsString); } else { program.InstallDate = this.win32APIClient.GetRegistryKeyLastModifiedDate(subkey.Handle); } program.InstallLocation = subkey.GetValue(ConstString.REGISTRY_INSTALL_LOCATION)?.ToString() ?? string.Empty; program.Version = subkey.GetValue(ConstString.REGISTRY_DISPLAY_VERSION)?.ToString() ?? string.Empty; installedPrograms.Add(program); } } catch (Exception ex) { this.Logger.LogError(ex.Message, ex); } } } } }
public void Start() { string txt = string.Format("事件時間:{0} 事件名稱:{1}", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), "服務啟動"); AppLib.WriteToFile(txt, logPath, false); //有檔案 並與目前取得清單比對(避免服務停止時有 if (File.Exists(programsPath) && File.Exists(publicKeyPath) && File.Exists(privateKeyPath)) { rsaCryption = new RSACryption(AppLib.ReadFromFile(publicKeyPath), AppLib.ReadFromFile(privateKeyPath)); ReadProgramsList(programsPath); List <InstalledProgram> nowInstalledPrograms = InstalledProgram.GetInstalledPrograms(); CompareAndWriteLog(nowInstalledPrograms, installedPrograms); } //第一次 or 遺失任一檔案 else { rsaCryption = new RSACryption(); AppLib.WriteToFile(rsaCryption.PublicKey, publicKeyPath); AppLib.WriteToFile(rsaCryption.PrivateKey, privateKeyPath); installedPrograms = InstalledProgram.GetInstalledPrograms(); WriteProgramsList(installedPrograms, programsPath); } //寫出目前取得的軟體名稱 Console.WriteLine("---------------------------------------"); Console.WriteLine("目前記錄取得{0}筆已安裝軟體", installedPrograms.Count); foreach (var item in installedPrograms) { Console.WriteLine(item.DisplayName); } Console.WriteLine("---------------------------------------"); //啟動監聽 WMIReceiveEvent(); }
/// <summary> /// Method to append the none-duplicated products from Uninstaller key /// We do this by iterating through the 64 and 32 bit Uninstall key @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" /// and @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall". /// Ignore all SystemComponent key names that have value of 1 /// Then we iterate through all user keys HKEY_USERS\user-sid\Software\Microsoft\Windows\CurrentVersion\Uninstall /// Then we add all none-duplicated products from @"Software\Microsoft\Windows\CurrentVersion\Installer\UserData\user-sid\Products\product-code\InstallProperties" /// </summary> /// <param name="tempInstalledProducts"></param> /// <param name="inHive"></param> /// <param name="uninstallerKeyPath"></param> /// <param name="in64or32key"></param> /// <returns></returns> public static List<InstalledProgram> AddProductsFromUninstallerKey(this List<InstalledProgram> tempInstalledProducts, UIntPtr inHive, string uninstallerKeyPath, RegSAM in64or32key) { var regex = new Regex("KB[0-9]{5,7}$"); int updatesCount = 0; List<string> subkeyNames = RegistryWrapper.GetSubKeyNames(inHive, uninstallerKeyPath, in64or32key); if (subkeyNames != null) { foreach (var subkeyName in subkeyNames) { // Check only keys like {CFEF48A8-BFB8-3EAC-8BA5-DE4F8AA267CE} // but not {CFEF48A8-BFB8-3EAC-8BA5-DE4F8AA267CE}.KB2504637 which are updates if (!regex.Match(subkeyName).Success) { string SystemComponent = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "SystemComponent"); if (SystemComponent != "1") { string Windowsinstaller = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "Windowsinstaller"); if (Windowsinstaller != "1") { //Make sure we are not dealing with a patch or an update string releaseType = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "ReleaseType"); string parentKeyName = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "ParentKeyName"); if (parentKeyName != null) { if (parentKeyName != "" || releaseType == "Security Update" || releaseType == "Update Rollup" || releaseType == "Hotfix" ) updatesCount++; } if (parentKeyName == null) { string uninstallString = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "UninstallString"); if (uninstallString != null) { string displayName = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "DisplayName"); string displayVersion = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "DisplayVersion"); string installDate = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "InstallDate"); if (displayName != null && displayName != "") { var product = new InstalledProgram(displayName, displayVersion, uninstallString, installDate); if (!tempInstalledProducts.Contains(product)) tempInstalledProducts.Add(product); } } } } else { string productId = GetInstallerKeyNameFromGuid(subkeyName); string productKey = @"Software\Classes\Installer\Products\" + productId; string productName = RegistryWrapper.GetRegKey(inHive, productKey, RegSAM.WOW64_64Key, "ProductName"); if (productName != null) { string displayVersion = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "DisplayVersion"); string uninstallString = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "UninstallString"); string installDate = RegistryWrapper.GetRegKey(inHive, uninstallerKeyPath + "\\" + subkeyName, in64or32key, "InstallDate"); var product = new InstalledProgram(productName, displayVersion, uninstallString, installDate); if (!tempInstalledProducts.Contains(product)) tempInstalledProducts.Add(product); } } } } } } return tempInstalledProducts; }
public static List<InstalledProgram> GetListOfInstalledPrograms() { string uninstallerKeyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"; //1. Get the products from 32bit Uninstaller @"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall" installedProgramsList.AddProductsFromUninstallerKey(RegHive.LocalMachine, uninstallerKeyPath, RegSAM.WOW64_32Key); //count = installedProductsList.Count; //Logger.Debug(count + @" products found in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"); //2. Get the products from 64bit Uninstaller @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" installedProgramsList.AddProductsFromUninstallerKey(RegHive.LocalMachine, uninstallerKeyPath, RegSAM.WOW64_64Key); //count = installedProductsList.Count - count; //Logger.Debug(count + @" products found in HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"); //count = installedProductsList.Count; //3. Get the products from HKEY_USERS\<user-sid>\Software\Microsoft\Windows\CurrentVersion\Uninstall string keyPath = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"; List<string> userKeys = RegistryWrapper.GetSubKeyNames(RegHive.LocalMachine, keyPath, RegSAM.WOW64_64Key); Logger.Debug(userKeys.Count + " user keys found in " + keyPath); if (userKeys != null) { foreach (var userKey in userKeys) { installedProgramsList.AddProductsFromUninstallerKey(RegHive.Users, userKey + "\\" + uninstallerKeyPath, RegSAM.WOW64_64Key); count = installedProgramsList.Count - count; Logger.Debug(count + " products found in HKEY_USERS\\" + userKey + "\\" + uninstallerKeyPath); count = installedProgramsList.Count; } } //4. Get the products from "Software\Microsoft\Windows\CurrentVersion\Installer\UserData\<user-sid>\Products\<product-code>\InstallProperties" keyPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData"; userKeys = RegistryWrapper.GetSubKeyNames(RegHive.LocalMachine, keyPath, RegSAM.WOW64_64Key); if (userKeys != null) { foreach (var userKey in userKeys) { if (userKey != "S-1-5-18") { //Get the products from i.e. "Software\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\<product-code>\InstallProperties" keyPath = @"Software\Microsoft\Windows\CurrentVersion\Installer\UserData\" + userKey + @"\Products\"; List<string> productKeys = RegistryWrapper.GetSubKeyNames(RegHive.LocalMachine, keyPath, RegSAM.WOW64_64Key); if (productKeys != null) { foreach (var productKey in productKeys) { string key = keyPath + productKey + @"\InstallProperties"; string systemComponent = RegistryWrapper.GetRegKey(RegHive.LocalMachine, key, RegSAM.WOW64_64Key, "SystemComponent"); if (systemComponent != "1") { string uninstallString = RegistryWrapper.GetRegKey(RegHive.LocalMachine, key, RegSAM.WOW64_64Key, "UninstallString"); string displayName = RegistryWrapper.GetRegKey(RegHive.LocalMachine, key, RegSAM.WOW64_64Key, "DisplayName"); string displayVersion = RegistryWrapper.GetRegKey(RegHive.LocalMachine, key, RegSAM.WOW64_64Key, "DisplayVersion"); string installDate = RegistryWrapper.GetRegKey(RegHive.LocalMachine, key, RegSAM.WOW64_64Key, "InstallDate"); if (displayName != null && displayName != "") { var installedProduct = new InstalledProgram(displayName, displayVersion, uninstallString, installDate); if (!installedProgramsList.Contains(installedProduct)) installedProgramsList.Add(installedProduct); } } } } } count = installedProgramsList.Count - count; Logger.Debug(count + " products found in HKEY_LOCAL_MACHINE\\" + keyPath); count = installedProgramsList.Count; } } // Add some logs for debugging if (Logger.level == (int)Logger.Level.DEBUG) { Logger.Debug("Products after Sort"); int i = 1; foreach (var p in installedProgramsList) { Logger.Debug(i + " " + p); i++; } } return installedProgramsList; }
/// <summary> /// Gets a list of installed programs /// </summary> /// <returns>The list of installed programs</returns> public List <InstalledProgram> GetInstalledPrograms() { List <InstalledProgram> installedPrograms = new List <InstalledProgram>(); try { RegistryKey installedRegistryPath = Registry.LocalMachine.OpenSubKey(_INSTALLED_PROGRAMS_PATH); RegistryKey userSpecificInstalledRegistryPath = Registry.CurrentUser.OpenSubKey(_INSTALLED_PROGRAMS_PATH); RegistryKey installedx86RegistryPath = Registry.LocalMachine.OpenSubKey(_INSTALLED_PROGRAMS_x86_KEY_PATH); string[] machineSpecificInstalledIds = installedRegistryPath.GetSubKeyNames(); string[] userSpecificInstalledIds = userSpecificInstalledRegistryPath.GetSubKeyNames(); string[] installedx86RegistryIds = installedx86RegistryPath.GetSubKeyNames(); foreach (string id in machineSpecificInstalledIds) { RegistryKey installedProgramRegEntry = Registry.LocalMachine.OpenSubKey(_INSTALLED_PROGRAMS_PATH + "\\" + id); InstalledProgram ip = ParseInstalledProgramFromKey(installedProgramRegEntry, id); if (ip.InstallDate == DateTime.MinValue) { ip.InstallDate = RegQueryInformationHelper.GetLastWritten(installedProgramRegEntry); } if (!installedPrograms.Contains(ip)) { installedPrograms.Add(ip); } } foreach (string id in userSpecificInstalledIds) { RegistryKey installedProgramRegEntry = Registry.CurrentUser.OpenSubKey(_INSTALLED_PROGRAMS_PATH + "\\" + id); InstalledProgram ip = ParseInstalledProgramFromKey(installedProgramRegEntry, id); if (ip.InstallDate == DateTime.MinValue) { ip.InstallDate = RegQueryInformationHelper.GetLastWritten(installedProgramRegEntry); } if (!installedPrograms.Contains(ip)) { installedPrograms.Add(ip); } } foreach (string id in installedx86RegistryIds) { RegistryKey installedProgramRegEntry = Registry.LocalMachine.OpenSubKey(_INSTALLED_PROGRAMS_x86_KEY_PATH + "\\" + id); InstalledProgram ip = ParseInstalledProgramFromKey(installedProgramRegEntry, id); if (ip.InstallDate == DateTime.MinValue) { ip.InstallDate = RegQueryInformationHelper.GetLastWritten(installedProgramRegEntry); } if (!installedPrograms.Contains(ip)) { installedPrograms.Add(ip); } } } catch (Exception exc) { MyDebugger.Instance.LogMessage(exc, DebugVerbocity.Exception); } return(installedPrograms); }
/// <summary> /// Parses the information about an installed program from the registry information /// </summary> /// <param name="installedProgramRegEntry">The registry subkey</param> /// <param name="id">The after which the subkey is named</param> /// <returns>The installed program</returns> private InstalledProgram ParseInstalledProgramFromKey(RegistryKey installedProgramRegEntry, string id) { InstalledProgram ip = new InstalledProgram(); foreach (string subkeyName in installedProgramRegEntry.GetValueNames()) { object subkeyValue = installedProgramRegEntry.GetValue(subkeyName); if (subkeyValue != null) { switch (subkeyName) { case ("DisplayName"): { ip.Name = subkeyValue.ToString(); break; } case ("InstallDate"): { try { ip.InstallDate = DateTime.ParseExact(subkeyValue.ToString(), new string[] { CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, "ddMMyyyy", "yyyyMMdd", "MMddyyyy", "yyyyddMM" }, CultureInfo.InvariantCulture, DateTimeStyles.None); } catch (Exception exc) { MyDebugger.Instance.LogMessage(exc, DebugVerbocity.Exception); ip.InstallDate = DateTime.MinValue; } break; } case ("Publisher"): { ip.Publisher = subkeyValue.ToString(); break; } case ("DisplayVersion"): { ip.DisplayVersion = subkeyValue.ToString(); break; } case ("InstallLocation"): { ip.InstallLocation = subkeyValue.ToString(); break; } case ("EstimatedSize"): { ip.EstimatedSize = Convert.ToUInt32(subkeyValue); break; } } } } if (ip.Name == null || ip.Name == "") { ip.Name = id; } return(ip); }