// takes the information at the given registry key and below it and copies it into the // given dictionary under the given key in the dictionary. private static void SaveRegistryKey(RegistryKey regKey, IDictionary saver, string dictKey) { // Since the RegistryKey class isn't serializable, we use our own class to hold the information. SerializableRegistryKey serializable = new SerializableRegistryKey(regKey); saver[dictKey] = serializable; }
public void CopyFromRegistry(RegistryKey keyToSave) { if (keyToSave == null) { throw new ArgumentNullException("keyToSave"); } ValueNames = keyToSave.GetValueNames(); //RegistryKey might return null on this call, if no values are //available. if (ValueNames == null) { ValueNames = new string[0]; } Values = new object[ValueNames.Length]; ValueKinds = new RegistryValueKind[ValueNames.Length]; for (int i = 0; i < ValueNames.Length; i++) { Values[i] = keyToSave.GetValue(ValueNames[i], null, RegistryValueOptions.DoNotExpandEnvironmentNames); ValueKinds[i] = keyToSave.GetValueKind(ValueNames[i]); } KeyNames = keyToSave.GetSubKeyNames(); //RegistryKey might return null on this call, if no values are //available. if (KeyNames == null) { KeyNames = new string[0]; } Keys = new SerializableRegistryKey[KeyNames.Length]; for (int i = 0; i < KeyNames.Length; i++) { Keys[i] = new SerializableRegistryKey(keyToSave.OpenSubKey(KeyNames[i])); } }
/// <include file='doc\PerformanceCounterInstaller.uex' path='docs/doc[@for="PerformanceCounterInstaller.Install"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public override void Install(IDictionary stateSaver) { base.Install(stateSaver); Context.LogMessage(Res.GetString(Res.CreatingPerformanceCounter, CategoryName)); RegistryKey categoryKey = null; RegistryKey subKey = null; RegistryKey servicesKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services", true); stateSaver["categoryKeyExisted"] = false; // save the old values of the Performance and Linkage subkeys in case we need to rollback the install try { if (servicesKey != null) { categoryKey = servicesKey.OpenSubKey(CategoryName, true); if (categoryKey != null) { stateSaver["categoryKeyExisted"] = true; subKey = categoryKey.OpenSubKey("Performance"); if (subKey != null) { stateSaver["performanceKeyData"] = new SerializableRegistryKey(subKey); subKey.Close(); categoryKey.DeleteSubKeyTree("Performance"); } subKey = categoryKey.OpenSubKey("Linkage"); if (subKey != null) { stateSaver["linkageKeyData"] = new SerializableRegistryKey(subKey); subKey.Close(); categoryKey.DeleteSubKeyTree("Linkage"); } } } } finally { if (categoryKey != null) { categoryKey.Close(); } if (servicesKey != null) { servicesKey.Close(); } } // delete the "Performance" and "Linkage" subkeys, unregister the category, // and finally re-create the category from scratch. if (PerformanceCounterCategory.Exists(CategoryName)) { PerformanceCounterCategory.Delete(CategoryName); } PerformanceCounterCategory.Create(CategoryName, CategoryHelp, categoryType, Counters); }
public override void Install(IDictionary stateSaver) { base.Install(stateSaver); base.Context.LogMessage(Res.GetString("CreatingPerformanceCounter", new object[] { this.CategoryName })); RegistryKey key = null; RegistryKey keyToSave = null; RegistryKey key3 = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services", true); stateSaver["categoryKeyExisted"] = false; try { if (key3 != null) { key = key3.OpenSubKey(this.CategoryName, true); if (key != null) { stateSaver["categoryKeyExisted"] = true; keyToSave = key.OpenSubKey("Performance"); if (keyToSave != null) { stateSaver["performanceKeyData"] = new SerializableRegistryKey(keyToSave); keyToSave.Close(); key.DeleteSubKeyTree("Performance"); } keyToSave = key.OpenSubKey("Linkage"); if (keyToSave != null) { stateSaver["linkageKeyData"] = new SerializableRegistryKey(keyToSave); keyToSave.Close(); key.DeleteSubKeyTree("Linkage"); } } } } finally { if (key != null) { key.Close(); } if (key3 != null) { key3.Close(); } } if (PerformanceCounterCategory.Exists(this.CategoryName)) { PerformanceCounterCategory.Delete(this.CategoryName); } PerformanceCounterCategory.Create(this.CategoryName, this.CategoryHelp, this.categoryType, this.Counters); }
/// <include file='doc\PerformanceCounterInstaller.uex' path='docs/doc[@for="PerformanceCounterInstaller.DoRollback"]/*' /> /// <devdoc> /// Restores the registry to how it was before install, as saved in the state dictionary. /// </devdoc> private void DoRollback(IDictionary state) { //we need to remove the category key OR put old data back into the key Context.LogMessage(Res.GetString(Res.RestoringPerformanceCounter, CategoryName)); using (RegistryKey servicesKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services", true)) { RegistryKey categoryKey = null; if ((bool)state["categoryKeyExisted"]) { categoryKey = servicesKey.OpenSubKey(CategoryName, true); if (categoryKey == null) { categoryKey = servicesKey.CreateSubKey(CategoryName); } categoryKey.DeleteSubKeyTree("Performance"); SerializableRegistryKey performanceKeyData = (SerializableRegistryKey)state["performanceKeyData"]; if (performanceKeyData != null) { RegistryKey performanceKey = categoryKey.CreateSubKey("Performance"); performanceKeyData.CopyToRegistry(performanceKey); performanceKey.Close(); } categoryKey.DeleteSubKeyTree("Linkage"); SerializableRegistryKey linkageKeyData = (SerializableRegistryKey)state["linkageKeyData"]; if (linkageKeyData != null) { RegistryKey linkageKey = categoryKey.CreateSubKey("Linkage"); linkageKeyData.CopyToRegistry(linkageKey); linkageKey.Close(); } } else { categoryKey = servicesKey.OpenSubKey(CategoryName); if (categoryKey != null) { categoryKey.Close(); categoryKey = null; servicesKey.DeleteSubKeyTree(CategoryName); } } if (categoryKey != null) { categoryKey.Close(); } } }
// Restores the registry to the state it was in when SaveRegistryKey ran. private void RestoreRegistryKey(SerializableRegistryKey serializable) { // A RegistryKey can't contain all the information necessary to serialize it, // so we have to have prior knowledge about where to put the saved information. // Luckily, we do. =) RegistryKey regKey = Registry.LocalMachine; regKey = regKey.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\EventLog", /*writable*/ true); regKey = regKey.CreateSubKey(Log); regKey = regKey.CreateSubKey(Source); serializable.CopyToRegistry(regKey); regKey.Close(); }
private void DoRollback(IDictionary state) { base.Context.LogMessage(Res.GetString("RestoringPerformanceCounter", new object[] { this.CategoryName })); using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services", true)) { RegistryKey key2 = null; if ((bool)state["categoryKeyExisted"]) { key2 = key.OpenSubKey(this.CategoryName, true); if (key2 == null) { key2 = key.CreateSubKey(this.CategoryName); } key2.DeleteSubKeyTree("Performance"); SerializableRegistryKey key3 = (SerializableRegistryKey)state["performanceKeyData"]; if (key3 != null) { RegistryKey baseKey = key2.CreateSubKey("Performance"); key3.CopyToRegistry(baseKey); baseKey.Close(); } key2.DeleteSubKeyTree("Linkage"); SerializableRegistryKey key5 = (SerializableRegistryKey)state["linkageKeyData"]; if (key5 != null) { RegistryKey key6 = key2.CreateSubKey("Linkage"); key5.CopyToRegistry(key6); key6.Close(); } } else { key2 = key.OpenSubKey(this.CategoryName); if (key2 != null) { key2.Close(); key2 = null; key.DeleteSubKeyTree(this.CategoryName); } } if (key2 != null) { key2.Close(); } } }
/// <include file='doc\PerformanceCounterInstaller.uex' path='docs/doc[@for="PerformanceCounterInstaller.DoRollback"]/*' /> /// <devdoc> /// Restores the registry to how it was before install, as saved in the state dictionary. /// </devdoc> private void DoRollback(IDictionary state) { //we need to remove the category key OR put old data back into the key Context.LogMessage(Res.GetString(Res.RestoringPerformanceCounter, CategoryName)); SerializableRegistryKey categoryKeyData = (SerializableRegistryKey)state["categoryKeyData"]; RegistryKey servicesKey = Registry.LocalMachine.CreateSubKey("SYSTEM\\CurrentControlSet\\Services"); RegistryKey categoryKey = servicesKey.OpenSubKey(CategoryName); if (categoryKey != null) { categoryKey.Close(); servicesKey.DeleteSubKeyTree(CategoryName); } //re-create key with old data ONLY if we had data saved into the hashtable if (categoryKeyData != null) { categoryKey = servicesKey.CreateSubKey(CategoryName); categoryKeyData.CopyToRegistry(categoryKey); } }
/// <include file='doc\PerformanceCounterInstaller.uex' path='docs/doc[@for="PerformanceCounterInstaller.Install"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public override void Install(IDictionary stateSaver) { base.Install(stateSaver); Context.LogMessage(Res.GetString(Res.CreatingPerformanceCounter, CategoryName)); RegistryKey categoryKey = null; RegistryKey servicesKey = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services", true); if (servicesKey != null) { categoryKey = servicesKey.OpenSubKey(CategoryName); if (categoryKey != null) { stateSaver["categoryKeyData"] = new SerializableRegistryKey(categoryKey); categoryKey.Close(); servicesKey.DeleteSubKeyTree(CategoryName); } } PerformanceCounterCategory.Create(CategoryName, CategoryHelp, Counters); }