1、写入注册表开机启动项
需引入命名空间using System.Windows.Forms;
string strAssName = Application.StartupPath + @"\" + Application.ProductName + @".exe";//获得应用程序路径
string ShortFileName = Application.ProductName;//获得应用程序名
RegistryKey rgkRun = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
try
{
if (rgkRun == null)
{
rgkRun = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");//写入注册表
}
rgkRun.SetValue(ShortFileName, strAssName);
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
finally
{
if(rgkRun != null)
{
rgkRun.Close();
}
}
msconfig查看执行效果: