安装程序自定义操作问题 - 无法写入注册表项
问题描述:
在自定义操作编辑器中,我已将自定义操作添加到进程的安装和卸载阶段。在属性窗口中我已经打上CustomActionData属性为:安装程序自定义操作问题 - 无法写入注册表项
/TARGETDIR = "[TARGETDIR]"
我希望以上通过安装目录信息到自定义操作。
自定义操作似乎被解雇,但我发现了以下错误消息:
“错误1001无法写入注册的钥匙”(或类似的东西,我翻译它来自我的当地语言)。
我在做什么错?
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();
}
}
}
答
尝试改变:RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path);
要:RegistryKey key = Registry.LocalMachine.OpenSubKey(key_path, Microsoft.Win32.RegistryKeyPermissionCheck.ReadWriteSubTree);
要安装我使用 “注册表视图” 里我改变注册表节点我的注册表项。 – 2009-11-23 11:19:49
你如何准确地设定他们的值? – Maciek 2009-11-23 13:10:25