C# 设置程序开机自动运行(+注册表项)

有时候我们需要让软件安装好了,开机自动运行,这时我们需要把启动项加载到注册表中,需要注意的时现在很多杀毒软件在其他软件更改注册表的时候会有提示,可能会阻止。下面代码包含增加启动项到注册表和删除启动项。

//此方法把启动项加载到注册表中 
//获得应用程序路径
string strAssName = Application.StartupPath + @"\" + Application.ProductName + @".exe";
//获得应用程序名 
string ShortFileName = Application.ProductName; RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (rgkRun == null)

    rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");

rgkRun.SetValue(ShortFileName, strAssName); 

//此删除注册表中启动项 
//获得应用程序名 
string ShortFileName = Application.ProductName; RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
if (rgkRun == null)

   rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
rgkRun.DeleteValue(ShortFileName, false);
posted @ 2013-09-07 00:45  petercao  阅读(3320)  评论(2编辑  收藏  举报