安装程序自定义操作的问题 - 不能写入注册键 [英] Installer Custom Action problem - can't write to register key

查看:189
本文介绍了安装程序自定义操作的问题 - 不能写入注册键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在自定义操作编辑,我已经添加了自定义操作过程中安装和卸载阶段。在属性窗口中我已经标出CustomActionData属性为:

  / TARGETDIR =[TARGETDIR]

我希望以上通过安装目录信息到自定义操作。

自定义操作似乎被解聘,但我发现了以下错误消息:

错误1001无法写入注册的钥匙的(或类似的东西,我从我的本地语言翻译的话)。

我在做什么错了?

 使用系统;
System.Collections中使用;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Configuration.Install;
使用System.Linq的;
使用System.Windows.Forms的//;
使用的Microsoft.Win32;命名空间CustomActions
{
    [runInstaller的(真)
    公共部分类Installer1:安装程序
    {
        公共覆盖无效安装(IDictionary的stateSaver)
        {
            base.Install(stateSaver);            常量字符串key_path =SOFTWARE \\\\ \\\\ VENDORNAME MyAppName
            常量字符串key_value_name =InstallationDirectory;            关键的RegistryKey = Registry.LocalMachine.OpenSubKey(key_path);            如果(键== NULL)
            {
                键= Registry.LocalMachine.CreateSubKey(key_path);
            }            字符串tgt_dir = Context.Parameters [TARGETDIR];            key.SetValue(key_value_name,tgt_dir);        }        公共覆盖无效卸载(IDictionary的savedState)
        {
            base.Uninstall(savedState);            常量字符串key_path =SOFTWARE \\\\ VENDORNAME
            常量字符串KEY_NAME =MyAppName;            关键的RegistryKey = Registry.LocalMachine.OpenSubKey(key_path);            如果(key.OpenSubKey(KEY_NAME)!= NULL)
            {
                key.DeleteSubKey(KEY_NAME);
            }
        }        公共覆盖无效回滚(IDictionary的savedState)
        {
            base.Rollback(savedState);
        }
        公共Installer1()
        {
            的InitializeComponent();
        }
    }
}


解决方案

尝试改变:结果
的RegistryKey键= Registry.LocalMachine.OpenSubKey(key_path);

要:结果
的RegistryKey键= Registry.LocalMachine.OpenSubKey(key_path,Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree);

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();
        }
    }
}

解决方案

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

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

这篇关于安装程序自定义操作的问题 - 不能写入注册键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆