1. 程式人生 > >WPF應用開機自啟動程式設計實現

WPF應用開機自啟動程式設計實現

不論是WinForm程式還是WPF程式,實現開機自啟動的原理都是向登錄檔中寫值,位置在登錄檔的“LocalMachine\SOFTWARE\Microsoft\Windows\CurrentVersion\Run”目錄下。
手動編輯登錄檔實現應用程式開機自啟動請檢視下面文章:

C#程式實現

1.判斷登錄檔鍵值對是否存在

        private bool IsExistKey(string keyName)
        {
            try
            {
                bool _exist = false;
                RegistryKey local = Registry.LocalMachine;
                RegistryKey runs = local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
if (runs == null) { RegistryKey key2 = local.CreateSubKey("SOFTWARE"); RegistryKey key3 = key2.CreateSubKey("Microsoft"); RegistryKey key4 = key3.CreateSubKey("Windows"); RegistryKey key5 = key4.CreateSubKey("CurrentVersion"
); RegistryKey key6 = key5.CreateSubKey("Run"); runs = key6; } string[] runsName = runs.GetValueNames(); foreach (string strName in runsName) { if (strName.ToUpper() == keyName.ToUpper()) { _exist = true; return
_exist; } } return _exist; } catch { return false; } }

寫入/刪除登錄檔鍵值對

        ///isStart--是否開機自啟動
        ///exeName--應用程式名
        ///path--應用程式路徑
        private static bool SelfRunning(bool isStart, string exeName, string path)
        {
            try
            {
                RegistryKey local = Registry.LocalMachine;
                RegistryKey key = local.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                if (key == null)
                {
                    local.CreateSubKey("SOFTWARE//Microsoft//Windows//CurrentVersion//Run");
                }
                if (isStart)//若開機自啟動則新增鍵值對
                {
                    key.SetValue(exeName, path);
                    key.Close();
                }
                else//否則刪除鍵值對
                {
                    string[] keyNames = key.GetValueNames();
                    foreach (string keyName in keyNames)
                    {
                        if (keyName.ToUpper() == exeName.ToUpper())
                        {
                            key.DeleteValue(exeName);
                            key.Close();
                        }
                    }
                }
            }
            catch (Exception)
            {
                return false;
                //throw;
            }

            return true;
        }

實現開機自啟動

namespace KSHServer
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        private static bool IsStart = Convert.ToBoolean(ConfigurationManager.AppSettings["IsStart"]);

        public MainWindow()
        {
            InitializeComponent();

            if (!IsExistKey("ServerApp") && IsStart)
            {
                SelfRunning(IsStart, "ServerApp", @"E:\ServerApp\ServerApp.exe");
            }
            else if (IsExistKey("ServerApp") && !IsStart)
            {
                SelfRunning(!IsStart, "ServerApp", @"E:\ServerApp\ServerApp.exe");
            }
        }

控制是否開機自啟動的欄位放在了配置檔案App.config中,根據需要修改即可(true:開機自啟動;false:取消開機自啟動)

  <appSettings>
    <!--是否開機自啟動-->
    <add key="IsStart" value="true"/>
  </appSettings>
namespace ServerApp
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        private static bool IsStart = Convert.ToBoolean(ConfigurationManager.AppSettings["IsStart"]);

        public MainWindow()
        {
            InitializeComponent();

            if (!IsExistKey("ServerApp") && IsStart)
            {
                SelfRunning(IsStart, "ServerApp", @"E:\ServerApp\ServerApp.exe");
            }
            else if (IsExistKey("ServerApp") && !IsStart)
            {
                SelfRunning(!IsStart, "ServerApp", @"E:\ServerApp\ServerApp.exe");
            }
        }

控制是否開機自啟動的欄位放在了配置檔案App.config中,根據需要修改即可(true:開機自啟動;false:取消開機自啟動)