C # ' da, var olabilecek veya olmayabilecek Kayıt Defteri anahtarlarını nasıl test edebilirim / alabilirim / ayarlayabilirim?

0

Soru

Bir anahtarı test etmem, bir anahtar ayarlamam ve bir anahtarı temizlemem gerekiyor ve her durumda tam yol ve anahtar değerleri gerçekten mevcut olmayabilir. Komutların, yolun bir kısmı kontrolde mevcut değilse false döndürerek ve mevcut değilse setteki yolu oluşturarak bunu açıklayacağını düşündüm, ancak durum böyle değil gibi görünüyor.

        
        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");
                }
            }
        }       

Bilmek istediğim, bu sorunu çözmenin en temiz yolu. Yolları parçalayan, her seviyeyi test eden ve zaten orada değilse alt anahtarı ekleyen hızlı bir işlev yazabilirim, ancak bunu yapmak için daha zarif veya yerleşik bir yol varsa bunu yapmamayı tercih ederim.

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

En iyi cevabı

0

Tamam, etrafta avlanıyordum ve bunu yapmanın temiz bir yolunu bulmuş olabilirim.

İlk olarak, test etmek için bir işlev oluşturdum ve gerekirse oluşturdum:

        // 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;
        }

Sonra bunu aşağıdaki gibi kullanıyorum:

        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();
        }

Bunun nasıl geliştirilebileceği/temizlenebileceği konusunda önerilere açığım.

2021-11-17 17:14:58

Diğer dillerde

Bu sayfa diğer dillerde

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