在 c# 中,如何测试/获取/设置可能存在或不存在的注册表项?

Posted

技术标签:

【中文标题】在 c# 中,如何测试/获取/设置可能存在或不存在的注册表项?【英文标题】:In c#, how do I test/get/set Registry keys that may or may not exist? 【发布时间】:2021-12-28 13:28:49 【问题描述】:

我需要测试一个键、设置一个键并清除一个键,在所有情况下,完整路径和键值可能实际上并不存在。我认为如果检查时部分路径不存在,则命令会通过返回 false 来解决此问题,如果路径不存在,则在设置时创建路径,但情况似乎并非如此。

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

所以我想知道的是处理这个问题的最干净的方法。我可以编写一个快速函数来分解路径,测试每个级别,并添加子键(如果它不存在),但如果有更优雅或内置的方法,我宁愿不这样做。

【问题讨论】:

【参考方案1】:

好的,我正在四处寻找,可能已经找到了一种干净的方法。

首先,我创建了一个函数来测试,然后在必要时创建:

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

我愿意接受任何可以改进/清理的建议。

【讨论】:

以上是关于在 c# 中,如何测试/获取/设置可能存在或不存在的注册表项?的主要内容,如果未能解决你的问题,请参考以下文章

在 Mongo Aggregation 中获取 ElemMatch 以查找 null 或不存在的数组对象

在Python中删除可能存在或不存在的字典属性[重复]

MySQL 中可能存在或不存在的引用数据

带有熊猫数据框的内连接循环,用于可能存在或不存在的各种组合[重复]

我的查询需要啥,可能不存在或不存在? [复制]

如何检查 ViewBag 属性是不是为空或不存在