在c#我怎么测试/get/set注册键,可能会或可能不存在吗?

0

的问题

我需要测试一个关键,设置一个关键,并明确一个关键和在所有情况下的全的道路和关键的价值观可能实际上不存在。 我认为该指令会考虑,通过返回假,如果一部分的道路并不存在对检查和创建的道路上设置的,如果它不存在,但这似乎不是这种情况。

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

所以我想知道的是最清洁的方式来处理这个问题。 我可以写一个快速的功能,打破的路径,测试每个级别,并增加了子项,如果它不在那里了,但是我宁愿不这样做,如果有一个更优雅的或建造的方式这样做。

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

最好的答案

0

好吧,我周围的狩猎和可能已经找到一个清洁的方式来做到这一点。

第一,我创建了一个功能测试,然后创建的,如果有必要的:

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

然后我用如下:

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

我很开放的建议的任何方式,这可以改进的/清理。

2021-11-17 17:14:58

其他语言

此页面有其他语言版本

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