În c#, cum pot testa/get/set cheile de Registry care poate exista sau nu?

0

Problema

Am nevoie pentru a testa o cheie, setați o cheie, și clar o cheie și, în toate cazurile, calea completă și valori-cheie s-ar putea, de fapt, nu există. M-am gândit la comenzi ar putea fi cauza de se întoarce false dacă o parte din cale nu există pentru a verifica și de a crea calea pe platou dacă aceasta nu există, dar care pare să nu fie cazul.

        
        internal bool DownloadGroupByOff()
        {
            using (RegistryKey hku = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                using (RegistryKey explore = hku.OpenSubKey(@"Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\{885A186E-A440-4ADA-812B-DB871B942259}"))
                {
                    if (GetValueInt(explore,"GroupView") == 0)
                        return true;
                }
            }
            return false;
        }

        public void DownloadGroupByEnable()
        {
            using (RegistryKey hku = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                using (RegistryKey explore = hku.OpenSubKey(@"Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\{885A186E-A440-4ADA-812B-DB871B942259}", true))
                {
                    explore.DeleteValue("GroupView");
                    explore.DeleteValue("Mode");
                }
            }
        }

        public void DownloadGroupByDisable()
        {
            using (RegistryKey hku = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                using (RegistryKey explore = hku.OpenSubKey(@"Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\{885A186E-A440-4ADA-812B-DB871B942259}", true))
                {
                    explore.SetValue("", "Downloads");
                    explore.SetValue("GroupView", "0");
                    explore.SetValue("Mode", "4");
                }
            }
        }       

Deci, ce aș vrea să știu este cel mai curat mod de a rezolva această problemă. Aș putea scrie o scurtă funcția care rupe căi de sus, teste fiecare nivel, și adaugă subcheia dacă aceasta nu există deja, dar aș prefera să nu faci asta dacă nu e mai elegant sau built-in mod de a face acest lucru.

c# registry windows
2021-11-17 14:52:08
1

Cel mai bun răspuns

0

Ok, am fost în jurul valorii de vânătoare și ar fi găsit o modalitate de a face asta.

În primul rând, am creat o funcție pentru a testa crea apoi, dacă este necesar:

        // Helper because apparently it won't do this on its own
        public RegistryKey openCreate(RegistryKey baseKey, string path)
        {
            RegistryKey test = baseKey.OpenSubKey(path);

            var reg = baseKey.OpenSubKey(path, true);
            if (reg == null)
            {
                reg = baseKey.CreateSubKey(path);
            }
            return reg;
        }

Atunci îl folosesc după cum urmează:

        internal bool DownloadGroupByOff()
        {
            // Using "using" to handle auto-close when it leaves this code block (so we don't have to manually close before returning)
            using (RegistryKey localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64))
            {
                using (RegistryKey explore = localMachine.OpenSubKey(@"Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\{885A186E-A440-4ADA-812B-DB871B942259}")){
                    if (explore == null || GetValueInt(explore, "GroupView") != 0)
                        return false;
                }               
                using (RegistryKey explore = localMachine.OpenSubKey(@"Software\Microsoft\Windows\Shell\Bags\AllFolders\ComDlg\{885A186E-A440-4ADA-812B-DB871B942259}")){
                    if (explore == null || GetValueInt(explore, "GroupView") != 0)
                        return false;
                }               
                using (RegistryKey explore = localMachine.OpenSubKey(@"Software\Microsoft\Windows\Shell\Bags\AllFolders\ComDlgLegacy\{885A186E-A440-4ADA-812B-DB871B942259}")){
                    if (explore == null || GetValueInt(explore, "GroupView") != 0)
                        return false;
                }
                return true;
            }
        }
        public void DownloadGroupByEnable()
        {
            RegistryKey localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            // Adding false to this command says not to throw an exception if the key doesn't exist - just ignore
            localMachine.DeleteSubKeyTree(@"Software\Microsoft\Windows\Shell\Bags\AllFolders\Shell\{885A186E-A440-4ADA-812B-DB871B942259}", false);
            localMachine.DeleteSubKeyTree(@"SOFTWARE\Microsoft\Windows\Shell\Bags\AllFolders\ComDlg\{885a186e-a440-4ada-812b-db871b942259}", false);
            localMachine.DeleteSubKeyTree(@"Software\Microsoft\Windows\Shell\Bags\AllFolders\ComDlgLegacy\{885A186E-A440-4ADA-812B-DB871B942259}", false);
            localMachine.Close();
        }

        public void DownloadGroupByDisable()
        {
            RegistryKey localMachine = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
            
            RegistryKey explore = openCreate(localMachine,@"SOFTWARE\Microsoft\Windows\Shell\Bags\AllFolders\ComDlg\{885a186e-a440-4ada-812b-db871b942259}");
            explore.SetValue("", "Downloads");
            explore.SetValue("GroupView", "0");
            explore.SetValue("Mode", "4");
            explore.Close();

            explore = openCreate(localMachine, @"SOFTWARE\Microsoft\Windows\Shell\Bags\AllFolders\ComDlgLegacy\{885a186e-a440-4ada-812b-db871b942259}");
            explore.SetValue("", "Downloads");
            explore.SetValue("GroupView", "0");
            explore.SetValue("Mode", "4");
            explore.Close();

            explore = openCreate(localMachine, @"SOFTWARE\Microsoft\Windows\Shell\Bags\AllFolders\Shell\{885a186e-a440-4ada-812b-db871b942259}");
            explore.SetValue("", "Downloads");
            explore.SetValue("GroupView", "0");
            explore.SetValue("Mode", "4");
            explore.Close();

            localMachine.Close();
        }

Sunt deschis la sugestii de orice fel acest lucru poate fi îmbunătățit/curățat.

2021-11-17 17:14:58

În alte limbi

Această pagină este în alte limbi

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................