0

I implemented installer to my WPF app by System.Configuration.Install.Installer class. I wanted to remove registryKey added by my app when uninstalling it, so I wrote follwing as a part of CustomAction.dll:

using System;

namespace CustomAction
{
    [System.ComponentModel.RunInstaller(true)]
    public class ActionSetting : System.Configuration.Install.Installer
    {
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            var name = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
            RemoveKey(name);

            base.Uninstall(savedState);
        }

        private void RemoveKey(string Key)
        {
            try
            {
                Microsoft.Win32.RegistryKey regkey =
                    Microsoft.Win32.Registry.CurrentUser.OpenSubKey(
                    @"Software\Microsoft\Windows\CurrentVersion\Run", true);
                regkey.DeleteValue(Key, false);
                regkey.Close();
            }
            catch
            {

            }
        }
    }
}

But it doesn't work. In practice, anything I wrote, regardless of the contents it doesn't seems to be called. I confirmed Uninstall method is definitely called, testing by some code like MessageBox.Show("uninstalled") . What should I do? I feel like I should find other way not using .dll. I use Visual Studio 2017. thanks.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Shintaro Nomiya
  • 85
  • 1
  • 11
  • 1
    I noticed my code is little wrong because Assembly.GetExecutingAssembly() returns project's name created for .dll (I mean, in this case it returns "CustomAction"). I fixed it, but problem was not solved. RegistryKey I wanted to delete still has remained... – Shintaro Nomiya Nov 04 '19 at 08:23
  • I think Key is a reserved keyword, so I'd rename this for a start. Hard one to suggest anything, maybe add some error logging in the catch and run process monitor to see what is happening in the registry. – Ryan Thomas Nov 04 '19 at 09:21
  • Thanks for your comment. Finally I found Microsoft.Win32.Registry.CurrentUser got not "HKEY_CURRENT_USER" but "HKEY_USERS\.Default" when uninstalling, therefore the key registered on "HKEY_CURRENT_USER" was not found. If I can know how to get "HKEY_CURRENT_USER" RegistryKey directory, I'd solve this issue, I guess. – Shintaro Nomiya Nov 04 '19 at 09:57
  • Looks like this https://stackoverflow.com/questions/39261491/write-registry-to-hkey-current-user-instead-of-hkey-users could be your problem then :) – Ryan Thomas Nov 04 '19 at 10:16
  • 1
    Oh, that's gonna be useful. I'm really grateful for your help! Thank you. – Shintaro Nomiya Nov 04 '19 at 11:26

0 Answers0