-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
84 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using System.IO; | ||
using Core.SDKs.Services; | ||
using log4net; | ||
using Microsoft.Win32; | ||
using PluginCore; | ||
|
||
namespace Core.Window; | ||
|
||
public class AutoStartService : IAutoStartService | ||
{ | ||
private static readonly ILog log = LogManager.GetLogger(nameof(AutoStartService)); | ||
|
||
public bool ChangeAutoStart(bool autoStart) | ||
{ | ||
if (autoStart) | ||
{ | ||
var strName = AppDomain.CurrentDomain.BaseDirectory + "KitopiaAvalonia.exe"; //获取要自动运行的应用程序名 | ||
if (!File.Exists(strName)) //判断要自动运行的应用程序文件是否存在 | ||
{ | ||
return false; | ||
} | ||
|
||
var registry = | ||
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", | ||
true); //检索指定的子项 | ||
if (registry == null) //若指定的子项不存在 | ||
{ | ||
registry = Registry.CurrentUser.CreateSubKey( | ||
"Software\\Microsoft\\Windows\\CurrentVersion\\Run"); //则创建指定的子项 | ||
} | ||
else | ||
{ | ||
if (Equals(registry.GetValue("Kitopia"), $"\"{strName}\"")) | ||
{ | ||
log.Info("开机自启配置已存在"); | ||
return true; | ||
} | ||
} | ||
|
||
log.Info("用户确认启用开机自启"); | ||
try | ||
{ | ||
registry.SetValue("Kitopia", $"\"{strName}\""); //设置该子项的新的“键值对” | ||
((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).Show("开机自启", | ||
"开机自启设置成功"); | ||
} | ||
catch (Exception exception) | ||
{ | ||
log.Error("开机自启设置失败"); | ||
log.Error(exception.StackTrace); | ||
((IToastService)ServiceManager.Services.GetService(typeof(IToastService))).Show("开机自启", | ||
"开机自启设置失败"); | ||
return false; | ||
} | ||
} | ||
else | ||
{ | ||
try | ||
{ | ||
var registry = | ||
Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run", | ||
true); //检索指定的子项 | ||
registry?.DeleteValue("Kitopia"); | ||
} | ||
catch (Exception) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Core.SDKs.Services; | ||
|
||
public interface IAutoStartService | ||
{ | ||
public bool ChangeAutoStart(bool autoStart); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters