c#应用程序自动启动Windows 7

本文关键字:Windows 自动启动 应用程序 | 更新日期: 2023-09-27 17:50:17

我有一个项目,我在其中添加了一个安装程序项目。我遵循这个(自定义安装程序)

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

无论如何,问题是:在visual studio中,添加键时不会产生错误(代码运行良好)(我编写了一个单独的应用程序来测试这一点)。然而,钥匙没有被写出来——我不明白为什么?

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

我确实尝试为当前用户设置密钥,而不是工作得很好,但它对我没有用处....

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

总结一下,我想弄明白的是:我怎么能抛出一个错误,注册表写入失败,并回滚安装(记住代码目前不会抛出一个错误下面在提升的特权,但实际上不工作)

这个问题有解决办法吗?

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

c#应用程序自动启动Windows 7

我猜这就是你的情况
在c#中添加注册表项会显示我何时读取它,但在regedit

中不会显示

如何确认
如注释中所建议的,使用ProcMon util,重定向情况下键读/写操作的结果将为REPARSE