Search code examples
c#registry

How do I delete ApplicationAssociationToasts subkey from registrary in c-sharp


I want to delete HKEY_CURRENT_USER > Software > Microsoft > Windows > CurrentVersion > ApplicationAssociationToasts > Applications\devenv.exe_.svg. For this I tried this code:

var np = @"Software\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts\" + $@"Applications\notepad++.exe_.svg";
Registry.CurrentUser.DeleteSubKey(np, false);

Here is the screenshot:

enter image description here

Bu it did not work.

Note: I don't want to delete subkeys of ApplicationAssociationToasts. I want to delete only Applications\notepad++.exe_.svg registrary that located under ApplicationAssociationToasts.

Edit: After the answer I tried like this:

  var parentKeyPath = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts\Applications\notepad++.exe_.svg";
  try
  {
      // Open the parent key with write access.
      using var parentKey = Registry.CurrentUser.OpenSubKey(parentKeyPath, writable: true);
      if (parentKey == null)
      {
          MessageBox.Show("The specified parent key does not exist.");
      }
      else
      {
          // Delete the "ApplicationAssociationToasts" subkey.
          parentKey.DeleteSubKey(@"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\ApplicationAssociationToasts\Applications\notepad++.exe_.svg");
          MessageBox.Show("Subkey 'ApplicationAssociationToasts' was successfully deleted.");
      }
  }
  catch (Exception ex)
  {
      // An exception can occur if the subkey does not exist or you do not have the necessary permissions.
      MessageBox.Show("An error occurred: " + ex.Message);
  }

Bu it is same. It gives The specified parent key does not exist.


Solution

  • Using the RegistryKey class from the.NET Framework, you can remove a subkey from the registry in C#. When making changes to the registry, use caution because making the wrong changes can lead to malfunctioning apps or the entire system. Prior to making any changes, make sure you have a backup of your registry.

    This is a simple example of removing the ApplicationAssociationToasts subkey from the registry. The subkey in this example is presumed to be in HKEY_CURRENT_USER\Software\ExampleSubkey. The path of your ApplicationAssociationToasts subkey should be used in place of ExampleSubkey.

    using System;
    using Microsoft.Win32;
    class Program
    {
        static void Main()
        {
            // Define the path to the parent key of the subkey you want to delete.
            // In this example, "Software\ExampleSubkey" is the parent key of "ApplicationAssociationToasts".
            string parentKeyPath = @"Software\ExampleSubkey";
    
            try
            {
                // Open the parent key with write access.
                using (RegistryKey parentKey = Registry.CurrentUser.OpenSubKey(parentKeyPath, writable: true))
                {
                    if (parentKey == null)
                    {
                        Console.WriteLine("The specified parent key does not exist.");
                    }
                    else
                    {
                        // Delete the "ApplicationAssociationToasts" subkey.
                        parentKey.DeleteSubKey("ApplicationAssociationToasts");
                        Console.WriteLine("Subkey 'ApplicationAssociationToasts' was successfully deleted.");
                    }
                }
            }
            catch (Exception ex)
            {
                // An exception can occur if the subkey does not exist or you do not have the necessary permissions.
                Console.WriteLine("An error occurred: " + ex.Message);
            }
        }
    }
    

    Prior to executing this code, please make sure you:

    You are authorised to make changes to the registry. It may be essential to run your application with administrative rights. The complete path of the subkey that you want to remove has been accurately determined. Make sure you are working in the correct root key in the registry, as there are numerous (such as HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE). You understand what will happen if you remove the subkey. Registry keys may be necessary for the setup and functionality of some apps. As usual, it's a good idea to test modifications on a non-critical environment or system to prevent unforeseen problems.