ใน c#ยังไงฉันทดสอบ/ได้/ตั้งค่าลงชื่อกุญแจนั่นอาจจะหรืออาจจะไม่มีอยู่จริงหรือ?

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

ดังนั้นสิ่งที่ฉันอยากจะรู้คือวิทยาลัยที่สะอาดที่สุดทางที่จะจัดการปัญหานี้ ฉันสามารถเขียนเป็นด่วนฟังก์ชันที่ทำลายทางขึ้นการทดสอบแต่ละระดับและเพิ่มค subkey ถ้ามันไม่ได้อยู่ที่นั่นแล้วแต่ฉันไม่อยากทำอย่างนั้นถ้ามีอีกแห่งเหล่าอัศวินราชินีและอสรหรือสร้างขึ้นในทางที่จะทำอย่างนั้น

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ý
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่