0

The purpose of my application is to provide services to many people across the globe. Due to the technical aspects; the modified changes that occur can be seen in the Developer Tools in all browsers. While I have figured out how to disable them, I am trying to replicate the process programmatically. The issue is, I was able to create the folder and subfolder of Mozilla\Firefox but then I am unable to create and set the value of the registry key. What am I doing wrong? Here is my code:

        public static void REGMODS()
        {
            try
            {
                RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Policies\Mozilla\Firefox");
                if (rk == null)
                {
                    MessageBox.Show("Something doesn't exists");
                    RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Policies\Mozilla\Firefox");
                    rk.SetValue("DisableDeveloperTools2", 1, RegistryValueKind.DWord);
                    rk.Close();
                }
                else
                {
                    MessageBox.Show("IT exists!");
                    //rk.CreateSubKey("DisableDeveloperTools2");
                    rk.SetValue("DisableDeveloperTools2", 1, RegistryValueKind.DWord);
                    rk.Close();
                }
            }
            catch (Exception ex)
            {
                // your exception handling process
            }
        }

I have tried using various locations, using rk.createsubkey and other various ways but none have been successful.

NGH
  • 19
  • 1
  • When trying the above, are you getting any particular exceptions on rk.SetValue? – DubDub May 10 '23 at 23:28
  • 1
    Just to mention, reading the Remarks section under the docs https://learn.microsoft.com/en-us/dotnet/api/microsoft.win32.registrykey.setvalue?view=net-7.0 it mentions to open the key with write access, and it looks like the OpenSubKey() method you're using has a few overloads which may sort this for you, however I can't be sure without the exception your getting. – DubDub May 10 '23 at 23:31
  • @DubDub after implementing the exception catch, it says it cannot write to the registry key > unauthrorizedAccess Exception.. – NGH May 10 '23 at 23:51
  • 1
    @DubDub thanks to your help, I was able to figure the issue out. – NGH May 10 '23 at 23:55
  • 2
    Side note: you need to close the keys with a `using` block, that includes `rk` – Charlieface May 11 '23 at 00:06
  • 1
    Does this answer your question? [Writing to registry in a C# application](https://stackoverflow.com/questions/7230102/writing-to-registry-in-a-c-sharp-application) – Charlieface May 11 '23 at 00:06

1 Answers1

0

I need to add an overload to my opensubkeys like this:

        public static void REGMODS()
    {
        try
        {
            RegistryKey rk = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Policies\Mozilla\Firefox", true);
            if (rk == null)
            {
                MessageBox.Show("Something doesn't exists");
                RegistryKey key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Policies\Mozilla\Firefox", true);
                rk.SetValue("DisableDeveloperTools2", 1, RegistryValueKind.DWord);
                rk.Close();
            }
            else
            {
                MessageBox.Show("IT exists!");
                //rk.CreateSubKey("DisableDeveloperTools2", true);
                //rk.CreateSubKey
                //rk.OpenSubKey("DisableDeveloperTools2", true);
                rk.SetValue("DisableDeveloperTools2", 1, RegistryValueKind.DWord);
                //rk.
                //rk.Close();
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("" + e);
        }
NGH
  • 19
  • 1