C#設置開機啟動項、取消開機啟動項


如果想你寫的程序隨系統開機一起啟動的話,那么你可以照下面這個方法來做。 

RunWhenStart(false, Application.ProductName, Application.StartupPath + @\"\\MUS.exe\");

/// <summary> 
/// 開機啟動項 
/// </summary> 
/// <param name=\"Started\">是否啟動</param> 
/// <param name=\"name\">啟動值的名稱</param> 
/// <param name=\"path\">啟動程序的路徑</param> 
public static void RunWhenStart(bool Started, string name, string path) 
{ 
  RegistryKey HKLM = Registry.LocalMachine; 
  RegistryKey Run = HKLM.CreateSubKey(@\"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\"); 
  if (Started == true) 
  { 
    try
    { 
      Run.SetValue(name, path); 
      HKLM.Close(); 
    } 
    catch (Exception Err) 
    { 
      MessageBox.Show(Err.Message.ToString(), \"MUS\", MessageBoxButtons.OK, MessageBoxIcon.Error); 
    } 
  } [Page]
  else
  { 
    try
    { 
      Run.DeleteValue(name); 
      HKLM.Close(); 
    } 
    catch (Exception) 
    { 
      // 
    } 
  } 
}

 

/// <summary>
/// 開機啟動項
/// </summary>
/// <param name=\"Started\">是否啟動</param>
/// <param name=\"name\">啟動值的名稱</param>
/// <param name=\"path\">啟動程序的路徑</param>
public static void RunWhenStart( bool Started, string name, string path)
{
   RegistryKey HKLM = Registry.LocalMachine;
   RegistryKey Run = HKLM.CreateSubKey(@\"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\");
   if (Started == true )
   {
     try
     {
       Run.SetValue(name, path);
       HKLM.Close();
     }
     catch (Exception Err)
     {
       MessageBox.Show(Err.Message.ToString(), \"MUS\", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
   } [Page]
   else
   {
     try
     {
       Run.DeleteValue(name);
       HKLM.Close();
     }
     catch (Exception)
     {
       //
     }
   }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM