C#設置開機啟動


原理就是在注冊表啟動項里添加一項。
路徑: SOFTWARE\Microsoft\Windows\CurrentVersion\Run
或者直接:運行->regedit找到這個路徑添加一項。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Microsoft.Win32;

namespace CSharpStart
{
     public  partial  class Form1 : Form
    {
         public Form1()
        {
            InitializeComponent();
        }

         private  void btnSet_Click( object sender, EventArgs e)
        {
            SetAutoRun( @" D:\CSharpStart.exe ", true);
        }
         ///   <summary>
        
///  設置應用程序開機自動運行
        
///   </summary>
        
///   <param name="fileName"> 應用程序的文件名 </param>
        
///   <param name="isAutoRun"> 是否自動運行,為false時,取消自動運行 </param>
        
///   <exception cref="System.Exception"> 設置不成功時拋出異常 </exception>
         public  static  void SetAutoRun( string fileName,  bool isAutoRun)
        {
            RegistryKey reg =  null;
             try
            {
                 if (!System.IO.File.Exists(fileName))
                     throw  new Exception( " 該文件不存在! ");
                String name = fileName.Substring(fileName.LastIndexOf( @" \ ") +  1);
                reg = Registry.LocalMachine.OpenSubKey( @" SOFTWARE\Microsoft\Windows\CurrentVersion\Run "true);
                 if (reg ==  null)
                    reg = Registry.LocalMachine.CreateSubKey( @" SOFTWARE\Microsoft\Windows\CurrentVersion\Run ");
                 if (isAutoRun)
                    reg.SetValue(name, fileName);
                 else
                    reg.SetValue(name,  false);
            }
             catch (Exception ex)
            {
                 throw  new Exception(ex.ToString());
            }
             finally
            {
                 if (reg !=  null)
                    reg.Close();
            }

        }
         // 另外也可以寫成服務,不過服務的話一般是在后台執行的,沒有程序界面。

    }
}
url: http://greatverve.cnblogs.com/archive/2012/02/18/csharp-autorun.html
參考:

C# winform程序設置開機啟動,當讀取配置文件,或者加載圖片如果設置的是相對路徑時,開機啟動時會出現問題(直接運程程序是沒問題的)。這是因為開機啟動的程序要使用絕對路徑,相對路徑不行。我們可以通過Application .StartupPath屬性經過處理得到文件的絕對路徑問題就解決了。

C# 通過讀寫注冊表來設置開機啟動想方法很簡單,網上很多:

///   <summary>          
///  開機啟動項        
///   </summary>        
///   <param name="Started"> 是否啟動 </param>          
///   <param name="name"> 啟動值的名稱 </param>           
///   <param name="path"> 啟動程序的路徑 </param>          
public  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 // 沒有權限會異常            
        { }
    }
     else
    {
         try
        {
            Run.DeleteValue(name);
            HKLM.Close();
        }
         catch // 沒有權限會異常 
        { }
    }
}

或者直接:

  1. //添加啟動
  2. RegistryKey ms_run = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
  3.                     ms_run.SetValue("mistysoft", Application.ExecutablePath.ToString());
  4. //刪除啟動(設為控,注冊表項還在)
  5. RegistryKey ms_run = Registry.LocalMachine.OpenSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run", true);
  6. ms_run.SetValue("mistysoft", "");

 


免責聲明!

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



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