简体   繁体   English

安装程序自定义操作问题 - 无法写入注册密钥

[英]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 : 在属性窗口中,我将CustomActionData属性标记为:

/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). “错误1001.无法写入注册表的密钥” (或者类似的东西,我正在用我的本地语言翻译它)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM