开机运行

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;

namespace SetStart
{    
public partial class Form1 : Form    
{        
public Form1()         {             InitializeComponent();         }

        private void btnAdd_Click(object sender, EventArgs e)         {             //获得文件的当前路径             string dir = Directory.GetCurrentDirectory();             //获取可执行文件的全部路径             string exeDir = dir + @"\SetStart.exe";

            string strName = Application.ExecutablePath;

            SetAutoRun(exeDir, true);         }         public static void SetAutoRun(string fileName, bool isAutoRun)         {             RegistryKey reg = null;             try             {                 if (!File.Exists(fileName))                 {                     throw new Exception("木有这个文件,搞什么搞");                 }                 string name = fileName.Substring(fileName.LastIndexOf(@"\") + 1);                 reg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//true 代表是开机运行。                 if (reg == null)                     reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");                 if (isAutoRun)                 {                     reg.SetValue(name, fileName);                     MessageBox.Show("开机可以黑屏了", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);                 }

                else                     reg.SetValue(name, false);             }             catch (Exception ex)             {

                throw new Exception(ex.ToString());             }             finally             {                 if (reg != null)                 {                     reg.Close();                 }             }

        }     } }

 

posted @ 2012-10-16 10:00  pnljs  阅读(258)  评论(0编辑  收藏  举报