From 15b38fe89221f204d75b73fc17c6f6fb01946e6e Mon Sep 17 00:00:00 2001 From: Anton Palgunov Date: Mon, 16 Jan 2023 23:18:01 +0000 Subject: [PATCH] Move from registry startup option to create a task. Close Program not auto starting. (Windows 11) #2 --- Program.cs | 52 ++++++++++++++++++++++++++++++-------- Properties/AssemblyInfo.cs | 4 +-- WinToLinux.csproj | 8 +++++- packages.config | 4 +++ 4 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 packages.config diff --git a/Program.cs b/Program.cs index 43f2063..769d27a 100644 --- a/Program.cs +++ b/Program.cs @@ -1,4 +1,4 @@ -using Microsoft.Win32; +using Microsoft.Win32.TaskScheduler; using System; using System.Collections.Generic; using System.Diagnostics; @@ -18,7 +18,6 @@ public static void Main() private readonly NotifyIcon trayIcon; private readonly ContextMenu trayMenu; - readonly RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); private readonly string appName = "WinToLinux"; List uefi = new List(); @@ -30,7 +29,6 @@ public static void Main() public SysTrayApp() { - bool isStart = registryKey.GetValue(appName) != null; GetMenuItems(); currentValue = bootsequence ?? uuid.First(); @@ -40,7 +38,7 @@ public SysTrayApp() trayMenu = new ContextMenu(); trayMenu.MenuItems.Add("Settings").Enabled = false; trayMenu.MenuItems.Add("-"); - trayMenu.MenuItems.Add("Start with system", OnRegisterInStartup).Checked = isStart; + trayMenu.MenuItems.Add("Start with system", OnRegisterInStartup).Checked = isTaskEnable(); trayMenu.MenuItems.Add("-"); trayMenu.MenuItems.Add("Reboot to...").Enabled = false; trayMenu.MenuItems.Add("-"); @@ -104,20 +102,52 @@ private void OnMenuClick(object sender, EventArgs e) } } - private void OnRegisterInStartup(object sender, EventArgs e) + private void CreateTask() { - bool isStartup = registryKey.GetValue(appName) == null; + using (TaskService ts = new TaskService()) + { + TaskDefinition td = ts.NewTask(); + + td.RegistrationInfo.Description = "WinToLinux. Start on boot"; + td.Triggers.Add(new LogonTrigger()); + td.Actions.Add(new ExecAction(Application.ExecutablePath, null, null)); + td.Principal.RunLevel = TaskRunLevel.Highest; + + ts.RootFolder.RegisterTaskDefinition(appName, td); + } + } - if (isStartup) + private void DeleteTask() + { + using (TaskService ts = new TaskService()) { - registryKey.SetValue(appName, Application.ExecutablePath); - trayMenu.MenuItems[2].Checked = true; + if (ts.GetTask(appName) != null) + { + ts.RootFolder.DeleteTask(appName); + } } - else + } + + private bool isTaskEnable() + { + using (TaskService ts = new TaskService()) + { + return (ts.GetTask(appName) != null); + } + } + + private void OnRegisterInStartup(object sender, EventArgs e) + { + if (isTaskEnable()) { - registryKey.DeleteValue(appName); + DeleteTask(); trayMenu.MenuItems[2].Checked = false; } + else + { + CreateTask(); + trayMenu.MenuItems[2].Checked = true; + } } private void OnReboot(object sender, EventArgs e) diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 13fb801..686a265 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ // Можно задать все значения или принять номера сборки и редакции по умолчанию // используя "*", как показано ниже: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] +[assembly: AssemblyVersion("1.1.0")] +[assembly: AssemblyFileVersion("1.1.0")] diff --git a/WinToLinux.csproj b/WinToLinux.csproj index 1dbbf2a..321396a 100644 --- a/WinToLinux.csproj +++ b/WinToLinux.csproj @@ -12,6 +12,7 @@ 512 true + false publish\ true Disk @@ -24,7 +25,6 @@ true 0 1.0.0.%2a - false false true @@ -59,10 +59,15 @@ WtL.ico + + + packages\TaskScheduler.2.10.1\lib\net452\Microsoft.Win32.TaskScheduler.dll + + @@ -83,6 +88,7 @@ + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/packages.config b/packages.config new file mode 100644 index 0000000..82f3e90 --- /dev/null +++ b/packages.config @@ -0,0 +1,4 @@ + + + + \ No newline at end of file