private static RegistryKey GetRootKey(HRootKeys RootKey) { RegistryKey KeyRoot = null; switch (RootKey) { case HRootKeys.HKEY_CLASSES_ROOT: KeyRoot = Registry.ClassesRoot; break; case HRootKeys.HKEY_CURRENT_CONFIG: KeyRoot = Registry.CurrentConfig; break; case HRootKeys.HKEY_CURRENT_USER: KeyRoot = Registry.CurrentUser; break; case HRootKeys.HKEY_PERFORMANCE_DATA: KeyRoot = Registry.PerformanceData; break; case HRootKeys.HKEY_LOCAL_MACHINE: KeyRoot = Registry.LocalMachine; break; case HRootKeys.HKEY_USERS: KeyRoot = Registry.Users; break; } return(KeyRoot); }
public static void SaveSetting(HRootKeys RootKey, string Section, string Name, object Value, RegistryValueKind Kind) { RegistryKey KeyRoot = GetRootKey(RootKey); RegistryKey KeySub = KeyRoot.OpenSubKey(Section, true); if (KeySub == null) { KeySub = KeyRoot.CreateSubKey(Section); } KeySub.SetValue(Name, Value, Kind); KeySub.Close(); }
public static void DeleteSetting(HRootKeys RootKey, string Section, string Name) { RegistryKey KeyRoot = GetRootKey(RootKey); using (RegistryKey KeySub = KeyRoot.OpenSubKey(Section, true)) { if (KeySub == null) { return; } if (KeySub.GetValue(Name) != null) { KeySub.DeleteValue(Name); } } }
public static object GetSetting(HRootKeys RootKey, string Section, string Name, object Default) { RegistryKey KeyRoot = GetRootKey(RootKey); RegistryKey KeySub = KeyRoot.OpenSubKey(Section, false); if (KeySub == null) { return(Default); } object Value = KeySub.GetValue(Name, Default); KeySub.Close(); return(Value); }
public static void DeleteSetting(HRootKeys RootKey, string Section, bool IsTree) { RegistryKey KeyRoot = GetRootKey(RootKey); if (KeyRoot.OpenSubKey(Section) == null) { return; } if (IsTree) { KeyRoot.DeleteSubKeyTree(Section); } else { KeyRoot.DeleteSubKey(Section); } }
/// <summary> /// 특정 확장자와 특정 파일을 연결시키거나 연결을 해제함. /// </summary> private static void LinkOrNotExtensionWithExecutable(string Extension, string ExecutableName, string ExecutableFullPath, bool Unlink) { //.dr 확장자 가진 파일을 C:\My\DoctorGu.exe와 연결시키기 HRootKeys[] aRootKey = new HRootKeys[] { HRootKeys.HKEY_CURRENT_USER, HRootKeys.HKEY_CLASSES_ROOT }; string[] aSectionRoot = new string[] { @"SOFTWARE\Classes\", "" }; for (int i = 0; i < aRootKey.Length; i++) { HRootKeys RootKey = aRootKey[i]; string SectionRoot = aSectionRoot[i]; //[HKEY_CLASSES_ROOT\.dr] //@="DoctorGu" if (!Unlink) { SaveSetting(RootKey, SectionRoot + Extension, "", ExecutableName, RegistryValueKind.String); } else { DeleteSetting(RootKey, SectionRoot + Extension); } //[HKEY_CLASSES_ROOT\DoctorGu] //@="DoctorGu File" // //[HKEY_CLASSES_ROOT\DoctorGu\shell\open\command] //@="C:\\My\\DoctorGu.exe \"%1\"" { string Section1 = SectionRoot + ExecutableName; string Section2 = SectionRoot + ExecutableName + @"\shell\open\command"; if (!Unlink) { SaveSetting(RootKey, Section1, "", ExecutableName + " File", RegistryValueKind.String); SaveSetting(RootKey, Section2, "", string.Format(@"{0} ""%1""", ExecutableFullPath), RegistryValueKind.String); } else { DeleteSetting(RootKey, Section1, true); } } } }
public static void DeleteSetting(HRootKeys RootKey, string Section) { bool IsTree = false; DeleteSetting(RootKey, Section, IsTree); }