In c#, come faccio a verificare/get/set di chiavi del Registro di sistema, che può o non esiste?

0

Domanda

Ho bisogno di un test di un tasto, impostare un tasto, e cancellare una chiave e in tutti i casi il percorso completo e il i valori di chiave potrebbero in realtà non esistono. Ho pensato che il comando possa conto che restituisce falso se fa parte del percorso non esiste e non esiste controllare e crea il percorso sul set se non esiste, ma che sembra non essere il caso.

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

Quindi quello che mi piacerebbe sapere è il modo più pulito per gestire questo problema. Potrei scrivere una funzione rapida che rompe i percorsi up, prove di ogni livello, e aggiunge la sottochiave se non c'è già, ma non vorrei fare che se c'è un modo più elegante o un modo per farlo.

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

Migliore risposta

0

Ok, ero a caccia in giro e potrebbe aver trovato un modo pulito per farlo.

Primo, ho creato una funzione per verificare, quindi, creare, se necessario:

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

Poi io lo uso come segue:

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

Sono aperto a suggerimenti per alcun modo, questo può essere migliorato/pulito.

2021-11-17 17:14:58

In altre lingue

Questa pagina è in altre lingue

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