0

during the installation of my program I want to set values in the registry under a specific key which already exists with a value I need to reset on uninstallation and also I need the old value in my program. I tried to solve this with custom actions:

    protected override void OnBeforeInstall(IDictionary savedState) {
        var originalCommand = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\myKey\Shell\Open\Command", null, "");
        Registry.SetValue(@"HKEY_CURRENT_USER\Software\myProgram\OriginalCommand", null, originalCommand);
        Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\myKey\Shell\Open\Command", null, "new value");
        base.OnBeforeInstall(savedState);
    }

    protected override void OnAfterRollback(IDictionary savedState) {
        ResetRegistryEntries();
        base.OnAfterRollback(savedState);
    }

    protected override void OnAfterUninstall(IDictionary savedState) {
        ResetRegistryEntries();
        base.OnAfterUninstall(savedState);
    }

    private void ResetRegistryEntries() {
        var originalCommand = Registry.GetValue(@"HKEY_CURRENT_USER\Software\myProgram\OriginalCommand", null, "");
        Registry.SetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\Classes\myKey\Shell\Open\Command", null, originalCommand);
    }

There are two strange things now.. first: If I install via visual studio (right click on my setup project and install) everything works fine.. my new value is set and the old value is also set on my "backup" registry key.. and when I uninstall everything is reset as it should. But.. if I install via the created .msi file the old value isnt set on my backup registry key.. but the new value is.. and the very stange thing.. when I uninstall everything is reset.. I wonder why and from where.

PhilDW
  • 20,260
  • 1
  • 18
  • 28
conan.ak
  • 1
  • 2

1 Answers1

0

I don't completely understand your sequence of events and exactly what you want, but:

  1. Even though a custom action is called "BeforeInstall" it is not before the install - it is after all the files have been installed and any registry entries in the IDE have been written. So you are overwriting any registry entries you may have set in the Registry view in the IDE.

  2. "After uninstall" is similarly misnamed. It is quite early in the uninstall process and before any registry values (installed with the IDE Registry view) are removed.

So your issue might be that you need a real "before install" to save registry values before they are created or overwritten by the install. You can verify the sequence by opening your MSI file with Orca, look at the InstallExecuteSequence table (click on Sequence heading to sort in the right order) and see where your custom actions are called (names will be hex strings starting with an underscore) relative to RemoveRegistryValues and WriteRegistryValues actions.

PhilDW
  • 20,260
  • 1
  • 18
  • 28