#winfrom应用程序的开机自启动功能
/// <summary>
/// 修改程序在注册表中的键值
/// </summary>
/// <param name="flag">1:开机启动</param>
private void StartUp(string flag)
{
string path = Application.StartupPath;
string keyName = path.Substring(path.LastIndexOf("\\") + 1);
Microsoft.Win32.RegistryKey Rkey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (flag.Equals("1"))
{
if (Rkey == null)
{
Rkey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
Rkey.SetValue(keyName, path + @"\WindowsFormsApplication2.exe");
}
else
{
if (Rkey != null)
{
Rkey.DeleteValue(keyName, false);
}
}
}
##以上代码在程序运行时、便可以实现开机自启动功能。