我有一个项目,我在其中添加了一个安装程序项目。我正在关注这个(自定义安装程序)
我添加了一个自定义安装程序类。这样做的一个目的是将程序添加到注册表中,以便在任何用户登录时自动启动。我以管理员身份运行 Visual Studio(在 Visual Studio 顶部这样说 - 注意:在计算机管理中我不是管理员)。然而,我的笔记本电脑也使用了一个名为 powerbroker 的应用程序。要安装应用程序,我右键单击并选择运行提升。从阅读其他帖子运行提升与管理员不同,这可能是问题所在。
无论如何,问题是:在Visual Studio中没有产生错误(代码运行良好)来添加密钥(我写了一个单独的应用程序来测试这个。但是密钥没有被写入——我不明白为什么?
当我将代码放入我的安装程序并运行提升时,不会抛出任何错误并且密钥也没有写入 - 至少如果它出错并回滚安装......
我确实尝试为当前用户设置密钥,并且效果很好,但对我没有用....
我还创建了一个本地用户,该用户是管理员的成员,并且也没有访问权限。
总结一下我想弄清楚的是:如何抛出注册表写入失败的错误并回滚安装(请记住,代码当前在提升的权限下不会抛出错误但实际上不起作用)
有解决这个问题的方法吗?
谢谢达摩
C# 安装程序类代码
using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Reflection;
using System.IO;
using Microsoft.Win32;
namespace OffLine.Installer
{
// Taken from:http://msdn2.microsoft.com/en-us/library/
// system.configuration.configurationmanager.aspx
// Set 'RunInstaller' attribute to true.
[RunInstaller(true)]
public class InstallerClass : System.Configuration.Install.Installer
{
public InstallerClass()
: base()
{
// Attach the 'Committed' event.
this.Committed += new InstallEventHandler(MyInstaller_Committed);
// Attach the 'Committing' event.
this.Committing += new InstallEventHandler(MyInstaller_Committing);
}
// Event handler for 'Committing' event.
private void MyInstaller_Committing(object sender, InstallEventArgs e)
{
// **** Beta Only **** Set program to autostart
try
{
RegistryKey add = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
add.SetValue("FactoryAuditEventNotification", "\"" + Application.ExecutablePath.ToString() + "\"");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
// Event handler for 'Committed' event.
private void MyInstaller_Committed(object sender, InstallEventArgs e)
{
try
{
Directory.SetCurrentDirectory(Path.GetDirectoryName
(Assembly.GetExecutingAssembly().Location));
Process.Start(Path.GetDirectoryName(
Assembly.GetExecutingAssembly().Location) + "\\FactoryAuditEventNotification.exe");
}
catch
{
// Do nothing...
}
}
// Override the 'Install' method.
public override void Install(IDictionary savedState)
{
base.Install(savedState);
}
// Override the 'Commit' method.
public override void Commit(IDictionary savedState)
{
base.Commit(savedState);
}
// Override the 'Rollback' method.
public override void Rollback(IDictionary savedState)
{
base.Rollback(savedState);
}
}
}