Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installer Custom Action problem - can't write to register key

In the Custom Actions editor I've added the custom action to Install and Uninstall stages of the process. In the properties window I've marked the CustomActionData property as :

/TARGETDIR = "[TARGETDIR]"

I'm hoping that the above passes the installation directory info into the custom action.

The custom action seems to be firing, but I'm getting the following error message :

"Error 1001. Can't write to register's key" (or something like that, I'm translating it from my local language).

What am I doing wrong?

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
//using System.Windows.Forms;
using Microsoft.Win32;

namespace CustomActions
{
    [RunInstaller(true)]
    public partial class Installer1 : Installer
    {
        public override void Install(IDictionary stateSaver)
        {
            base.Install(stateSaver);

            const string key_path = "SOFTWARE\\VendorName\\MyAppName";
            const string key_value_name = "InstallationDirectory";

            RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path);

            if (key == null)
            {
                key = Registry.LocalMachine.CreateSubKey(key_path);
            }

            string tgt_dir = Context.Parameters["TARGETDIR"];

            key.SetValue(key_value_name, tgt_dir);

        }

        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);

            const string key_path = "SOFTWARE\\VendorName";
            const string key_name = "MyAppName";

            RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path);

            if (key.OpenSubKey(key_name) != null)
            {
                key.DeleteSubKey(key_name);
            }
        }

        public override void Rollback(IDictionary savedState)
        {
            base.Rollback(savedState);
        }


        public Installer1()
        {
            InitializeComponent();
        }
    }
}
like image 362
Maciek Avatar asked Nov 23 '09 11:11

Maciek


1 Answers

Try to change:
RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path);

To:
RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree);

like image 128
Michel van Engelen Avatar answered Nov 04 '22 12:11

Michel van Engelen