RegistryControllerGetRegistryValues Method |
Namespace: Xcalibur.Controllers
public static string[] GetRegistryValues( RegistryHive root, string path, string computername, RegistryView viewType = RegistryView.Registry64 )
1// Main 2static void Main(string[] args) 3{ 4 // Get start up application summary 5 var summary = GetStartUpApplicationSummary(".", "64-bit"); 6 7 // Print 8 foreach(var item in summary) 9 { 10 Console.WriteLine(item.SubKey); 11 } 12 13 // Hold 14 Console.Read(); 15} 16 17/* Result Example: 18 * 19 * Adobe Creative Cloud 20 * iTunesHelper 21 * GoogleDriveSync 22 */ 23 24---------------------------------------------------------- 25 26private const string Path64 = @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Run"; 27private const string Path32 = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; 28private const string PathCurrentUser = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"; 29 30/// <summary> 31/// Gets the start up application summary. 32/// </summary> 33/// <param name="computername">The computername.</param> 34/// <param name="architecture">The architecture.</param> 35/// <returns></returns> 36public static ISubKeyItem[] GetStartUpApplicationSummary( 37 string computername, string architecture) 38{ 39 // Get OS subkeys 40 return GetOperatingSystemKeys( 41 computername, architecture, Path32, Path64, PathCurrentUser); 42} 43 44/// <summary> 45/// Sub key item interface. 46/// </summary> 47public interface ISubKeyItem 48{ 49 /// <summary> 50 /// Gets or sets the hive. 51 /// </summary> 52 /// <value> 53 /// The hive. 54 /// </value> 55 RegistryHive Hive { get; set; } 56 57 /// <summary> 58 /// Gets or sets the key path. 59 /// </summary> 60 /// <value> 61 /// The key path. 62 /// </value> 63 string KeyPath { get; set; } 64 65 /// <summary> 66 /// Gets or sets the sub key. 67 /// </summary> 68 /// <value> 69 /// The sub key. 70 /// </value> 71 string SubKey { get; set; } 72} 73 74/// <summary> 75/// Sub key item. 76/// </summary> 77public class SubKeyItem : ISubKeyItem 78{ 79 /// <summary> 80 /// Gets or sets the hive. 81 /// </summary> 82 /// <value> 83 /// The hive. 84 /// </value> 85 public RegistryHive Hive { get; set; } 86 87 /// <summary> 88 /// Gets or sets the key path. 89 /// </summary> 90 /// <value> 91 /// The key path. 92 /// </value> 93 public string KeyPath { get; set; } 94 95 /// <summary> 96 /// Gets or sets the sub key. 97 /// </summary> 98 /// <value> 99 /// The sub key. 100 /// </value> 101 public string SubKey { get; set; } 102} 103 104/// <summary> 105/// Builds the sub key items. 106/// </summary> 107/// <param name="hive">The hive.</param> 108/// <param name="keyPath">The key path.</param> 109/// <param name="apps">The apps.</param> 110/// <returns></returns> 111public static IEnumerable<SubKeyItem> BuildSubKeyItems(RegistryHive hive, string keyPath, 112 IEnumerable<string> apps) 113{ 114 return apps.Select(app => new SubKeyItem 115 { 116 Hive = hive, 117 KeyPath = keyPath, 118 SubKey = app 119 }).ToArray(); 120} 121 122/// <summary> 123/// Gets the operating system keys. 124/// </summary> 125/// <param name="computername">The computername.</param> 126/// <param name="architecture">The architecture.</param> 127/// <param name="path32">The path32.</param> 128/// <param name="path64">The path64.</param> 129/// <param name="pathCurrentUser">The path current user.</param> 130/// <returns></returns> 131public static ISubKeyItem[] GetOperatingSystemKeys( 132 string computername, string architecture, 133 string path32, string path64, string pathCurrentUser = null) 134{ 135 var subKeyItems = new List<ISubKeyItem>(); 136 137 // 64-bit only 138 if (architecture == "64-bit") 139 { 140 var apps64 = RegistryController.GetRegistryValues(RegistryHive.LocalMachine, path64, computername); 141 if (apps64 != null) 142 { 143 subKeyItems.AddRange(RegistryOperation.BuildSubKeyItems(RegistryHive.LocalMachine, path64, apps64)); 144 } 145 } 146 147 // 32-bit Applications 148 var apps32 = RegistryController.GetRegistryValues(RegistryHive.LocalMachine, path32, computername); 149 if (apps32 != null) 150 { 151 subKeyItems.AddRange(RegistryOperation.BuildSubKeyItems(RegistryHive.LocalMachine, path32, apps32)); 152 } 153 154 // Current User not specified 155 if (pathCurrentUser == null) return subKeyItems.ToArray(); 156 157 // Current User Applications 158 var appsCurrUser = RegistryController.GetRegistryValues( 159 RegistryHive.CurrentUser, pathCurrentUser, computername); 160 if (appsCurrUser != null) 161 { 162 subKeyItems.AddRange(RegistryOperation.BuildSubKeyItems(RegistryHive.CurrentUser, 163 pathCurrentUser, appsCurrUser)); 164 } 165 166 // Return 167 return subKeyItems.ToArray(); 168}