簡體   English   中英

使應用程序在啟動時運行

[英]Making an application run on startup

我一直在嘗試讓我的應用在啟動時運行。 我添加了一個上下文菜單,該菜單應打開和關閉此功能。 上下文菜單的左側啟用了“檢查”功能(選中后會得到一個檢查標記)。

    // 
    // menu_startup
    // 
    this.menu_startup.Name = "menu_startup";
    this.menu_startup.ShortcutKeyDisplayString = "";
    this.menu_startup.Size = new System.Drawing.Size(201, 22);
    this.menu_startup.Text = "Run on startup";
    this.menu_startup.Click += new System.EventHandler(this.menu_startup_Click);

這就是我在Form1.cs中所做的

public string regKey = "IMGit";

        public Form1()
        {
            InitializeComponent();
            notifyIcon1.ContextMenuStrip = contextMenuStrip1;

            RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            if (rkApp.GetValue(this.regKey) == null)
            {
                this.menu_startup.Checked = false;
            }
            else
            {
                this.menu_startup.Checked = true;
            }

            this.menu_about.Click += menu_about_Click; // Ignore
            this.menu_exit.Click += menu_exit_Click; // Ignore
            this.menu_startup.Click += menu_startup_Click;
        }    

            private void menu_startup_Click(object sender, EventArgs e)
            {
                RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

                if (rkApp.GetValue(this.regKey) == null)
                {
                    rkApp.SetValue(this.regKey, Application.ExecutablePath.ToString());
                }
                else
                {
                    rkApp.DeleteValue(this.regKey, false);
                }
            }

我看不到我在做什么錯。 這應該為我的應用設置一個新的注冊表項。

如果添加代碼行以在構造函數中創建注冊表項,它將創建該項。

想法?

如果要在應用程序啟動時創建注冊表項,則需要從構造函數中調用menu_startup_Click方法。

public Form1()
        {
            InitializeComponent();
            notifyIcon1.ContextMenuStrip = contextMenuStrip1;

            //Make the call to add the registry key here (plus Check or Uncheck the menu)
            menu_startup_Click(null,null); 

            this.menu_startup.Click += menu_startup_Click;                
        }    


 private void menu_startup_Click(object sender, EventArgs e)
        {
            RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

            //Check or Uncheck the menu
            this.menu_startup.Checked = (rkApp.GetValue(this.regKey) == null)

            if (rkApp.GetValue(this.regKey) == null)
            {
                rkApp.SetValue(this.regKey, Application.ExecutablePath.ToString());
            }
            else
            {
                rkApp.DeleteValue(this.regKey, false);
            }               
        }

在構造函數中創建lambda函數可解決此問題。

    this.menu_startup.Click += (s, ea) =>
    {
        RegistryKey rkApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        string appPath = Application.ExecutablePath.ToString();

        this.menu_startup.Checked = (rkApp.GetValue(this.regKey) == null);

        if (rkApp.GetValue(this.regKey) == null)
        {
            rkApp.SetValue(this.regKey, appPath);
        }
        else
        {
            rkApp.DeleteValue(this.regKey, false);
        } 
    };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM