From 81f3ad0fdc9b840bcbfdae0be9d10a671593f8e5 Mon Sep 17 00:00:00 2001 From: liufei Date: Wed, 21 Jul 2021 11:15:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=BC=80=E6=9C=BA=E8=87=AA?= =?UTF-8?q?=E5=90=AF=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App.xaml.cs | 92 ++----------------- Constant/Constants.cs | 3 + Control/UserControls/Config/AboutControl.xaml | 6 +- Control/UserControls/Config/OtherControl.xaml | 45 +++++++++ .../UserControls/Config/OtherControl.xaml.cs | 37 ++++++++ Control/Windows/ConfigWindow.xaml | 15 +++ Control/Windows/ConfigWindow.xaml.cs | 4 + GeekDesk.csproj | 8 ++ MainWindow.xaml.cs | 5 + Resource/Dictionary/Geometry.xaml | 12 +++ Util/RegisterUtil.cs | 82 +++++++++++++++++ ViewModel/AppConfig.cs | 30 ++++++ 12 files changed, 251 insertions(+), 88 deletions(-) create mode 100644 Control/UserControls/Config/OtherControl.xaml create mode 100644 Control/UserControls/Config/OtherControl.xaml.cs create mode 100644 Util/RegisterUtil.cs diff --git a/App.xaml.cs b/App.xaml.cs index 4c7ba9a..9446f2b 100644 --- a/App.xaml.cs +++ b/App.xaml.cs @@ -1,4 +1,5 @@ -using Microsoft.Win32; +using GeekDesk.Constant; +using Microsoft.Win32; using System; using System.IO; using System.Windows; @@ -23,96 +24,17 @@ namespace GeekDesk { bool ret; - mutex = new System.Threading.Mutex(true, "GeekDesk", out ret); + mutex = new System.Threading.Mutex(true, Constants.MY_NAME, out ret); if (!ret) { Environment.Exit(0); } - #region 设置程序开机自动运行(+注册表项) - try - { - //SetSelfStarting(true, "GeekDesk.exe"); - } - catch (Exception ex) - { - } - - #endregion } - - - - #region 注册表开机自启动 - - - /// - /// 开机自动启动 - /// - /// 设置开机启动,或取消开机启动 - /// 注册表中的名称 - /// 开启或停用是否成功 - public bool SetSelfStarting(bool started, string exeName) - { - RegistryKey key = null; - try - { - string exeDir = System.Windows.Forms.Application.ExecutablePath; - //RegistryKey HKLM = Registry.CurrentUser; - //key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项 - key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//打开注册表子项 - - if (key == null)//如果该项不存在的话,则创建该子项 - { - key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); - } - if (started) - { - try - { - object ob = key.GetValue(exeName, -1); - - if (!ob.ToString().Equals(exeDir)) - { - if (!ob.ToString().Equals("-1")) - { - key.DeleteValue(exeName);//取消开机启动 - } - key.SetValue(exeName, exeDir);//设置为开机启动 - } - key.Close(); - - } - catch (Exception ex) - { - return false; - } - } - else - { - try - { - key.DeleteValue(exeName);//取消开机启动 - key.Close(); - } - catch (Exception ex) - { - return false; - } - } - return true; - } - catch (Exception ex) - { - if (key != null) - { - key.Close(); - } - return false; - } - } - - #endregion } + + + + // private void WriteLog(object exception) // { // Exception ex = exception as Exception; diff --git a/Constant/Constants.cs b/Constant/Constants.cs index 17cf5c1..895b1f7 100644 --- a/Constant/Constants.cs +++ b/Constant/Constants.cs @@ -9,6 +9,9 @@ namespace GeekDesk.Constant public class Constants { public static string APP_DIR = AppDomain.CurrentDomain.BaseDirectory.Trim(); + + public static string MY_NAME = "GeekDesk"; + /// /// app数据文件路径 /// diff --git a/Control/UserControls/Config/AboutControl.xaml b/Control/UserControls/Config/AboutControl.xaml index 83d2329..e05472d 100644 --- a/Control/UserControls/Config/AboutControl.xaml +++ b/Control/UserControls/Config/AboutControl.xaml @@ -25,7 +25,7 @@ Command="hc:ControlCommands.OpenLink" CommandParameter="https://github.com/Demo-Liu/GeekDesk" Margin="0,0,10,0" Color="#24292F"/> - @@ -63,7 +63,7 @@ - + diff --git a/Control/UserControls/Config/OtherControl.xaml b/Control/UserControls/Config/OtherControl.xaml new file mode 100644 index 0000000..55c80b1 --- /dev/null +++ b/Control/UserControls/Config/OtherControl.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Control/UserControls/Config/OtherControl.xaml.cs b/Control/UserControls/Config/OtherControl.xaml.cs new file mode 100644 index 0000000..ed293c0 --- /dev/null +++ b/Control/UserControls/Config/OtherControl.xaml.cs @@ -0,0 +1,37 @@ +using GeekDesk.Constant; +using GeekDesk.Util; +using GeekDesk.ViewModel; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Windows.Navigation; +using System.Windows.Shapes; + +namespace GeekDesk.Control.UserControls.Config +{ + /// + /// OtherControl.xaml 的交互逻辑 + /// + public partial class OtherControl : UserControl + { + public OtherControl() + { + InitializeComponent(); + } + + private void SelfStartUpBox_Click(object sender, RoutedEventArgs e) + { + AppConfig appConfig = MainWindow.appData.AppConfig; + RegisterUtil.SetSelfStarting(appConfig.SelfStartUp, Constants.MY_NAME); + } + } +} diff --git a/Control/Windows/ConfigWindow.xaml b/Control/Windows/ConfigWindow.xaml index 2d0aadf..18a4478 100644 --- a/Control/Windows/ConfigWindow.xaml +++ b/Control/Windows/ConfigWindow.xaml @@ -103,6 +103,21 @@ /> + + +