c#application auto start windows 7

时间:2012-12-29 14:34:27

标签: c# windows windows-installer uac

我有一个项目,其中我添加了一个安装程序项目。我跟着这个(custom installer

我添加了自定义安装程序类。这样做的一个目的是在任何用户登录时将程序添加到注册表以进行自动启动。我以管理员身份运行visual studio(在visual studio的顶部说明 - 注意:在计算机管理中我不是管理员)。然而我的笔记本电脑也使用名为powerbroker的应用程序。要安装应用程序,我右键单击并选择运行提升。从阅读其他帖子来看,运行提升与管理员不同,其中可能存在问题。

无论如何,问题在于: 在visual studio中没有生成错误(代码运行正常)来添加密钥(我写了一个单独的应用程序来测试它。然而密钥却没有写入 - 我不明白为什么?

当我将代码放入我的安装程序并运行提升时,不会抛出错误并且也不会写入密钥 - 至少如果它出错并回滚安装....

我确实尝试为当前用户设置密钥而且工作正常,但它对我没用...

我还创建了一个本地用户,他是管理员的成员,也没有访问权限。

总结一下我想弄清楚的是: 如何抛出注册表写入失败的错误并回滚安装(请记住,代码目前在提升的权限下不会抛出错误,但实际上并不起作用)

这个问题有解决方法吗?

感谢 DAMO

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

1 个答案:

答案 0 :(得分:1)

我想这是你的情况 Adding registry key in C# shows when I read it back, but not in regedit

如何确认
正如注释中所建议的那样,使用ProcMon util,重定向时的键读/写操作结果将是 REPARSE

相关问题