[csharp]
/// <summary>
/// 設置程序開機啟動
/// 或取消開機啟動
/// </summary>
/// <param name="started">設置開機啟動,或者取消開機啟動</param>
/// <param name="exeName">注冊表中程序的名字</param>
/// <param name="path">開機啟動的程序路徑</param>
/// <returns>開啟或則停用是否成功</returns>
public static bool runWhenStart(bool started, string exeName, string path)
{
RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打開注冊表子項
if (key == null)//如果該項不存在的話,則創建該子項
{
key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
}
if (started == true)
{
try
{
key.SetValue(exeName, path);//設置為開機啟動
key.Close();
}
catch
{
return false;
}
}
else
{
try
{
key.DeleteValue(exeName);//取消開機啟動
key.Close();
}
catch
{
return false;
}
}
return true;
}
作者:pridescc