您的位置:首页 > 编程语言 > C#

C#程序开机自启以及最小化到托盘

2014-07-05 09:58 375 查看
      private void button1_Click(object sender, EventArgs e)

        {

            if (checkBox1.Checked == true)

            {

                //获取程序执行路径..

                string starupPath = Application.ExecutablePath;

                //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.

                RegistryKey loca = Registry.LocalMachine;

                RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

                try

                {

                    //SetValue:存储值的名称

                    run.SetValue("qidong", starupPath);

                    /// MessageBox.Show("已启用开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    loca.Close();

                }

                catch (Exception ee)

                {

                    MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

            else

            {

                // MessageBox.Show("没有选中");

                //获取程序执行路径..

                string starupPath = Application.ExecutablePath;

                //class Micosoft.Win32.RegistryKey. 表示Window注册表中项级节点,此类是注册表装.

                RegistryKey loca = Registry.LocalMachine;

                RegistryKey run = loca.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");

                try

                {

                    //SetValue:存储值的名称

                    run.DeleteValue("qidong");

                    MessageBox.Show("已停止开机运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    loca.Close();

                }

                catch (Exception ee)

                {

                    MessageBox.Show(ee.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }

            }

        }

        private void button2_Click(object sender, EventArgs e)

        {

            notifyIcon1.Visible = true;

            this.Hide();   

        }

        private void notifyIcon1_MouseDown(object sender, MouseEventArgs e)

        {

            this.Show();

           // this.WindowState = FormWindowState.Maximized;

            notifyIcon1.Visible = false;

            this.ShowInTaskbar = true;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C# 自启 托盘