【轉】C#設置Windows啓動項

轉自:http://www.csframework.com/archive/2/arc-2-20121231-1969.htmspa

 

/// <summary> 
/// Windows啓動項目管理 
/// </summary> 
public class WinStartItems
{
   const string REG_PATH = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
   
   /// <summary> 
   /// 取程序安裝位置 
   /// </summary> 
   /// <param name="registName">鍵名</param> 
   /// <returns></returns> 
   public static string GetRegistData(string registName)
   {
      string registData;
      RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_PATH, true);
      registData = ConvertEx.ToString(key.GetValue(registName));
      return registData;
   }
   
   /// <summary> 
   /// 取註冊表啓動項的啓動項目名稱 
   /// </summary> 
   /// <returns></returns> 
   public static string[] GetRegistName()
   {
      RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_PATH, true);
      return key.GetValueNames();
   }
   
   /// <summary> 
   /// 將程序的開機啓動寫入註冊表 
   /// </summary> 
   /// <param name="runName">啓動項目名稱</param> 
   /// <param name="starupPath">程序文件名</param> 
   /// <returns></returns> 
   public static bool RegistStartItem(string runName, string starupPath)
   {
      try
      {
         RegistryKey key = Registry.LocalMachine.OpenSubKey(REG_PATH, true);
         key.SetValue(runName, starupPath);
         return true;
      }
      catch
      {
         return false;
      }
   }
   
}
相關文章
相關標籤/搜索