Skip to content

Commit 15b38fe

Browse files
committed
Move from registry startup option to create a task. Close Program not auto starting. (Windows 11) #2
1 parent 2403588 commit 15b38fe

4 files changed

Lines changed: 54 additions & 14 deletions

File tree

Program.cs

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Microsoft.Win32;
1+
using Microsoft.Win32.TaskScheduler;
22
using System;
33
using System.Collections.Generic;
44
using System.Diagnostics;
@@ -18,7 +18,6 @@ public static void Main()
1818

1919
private readonly NotifyIcon trayIcon;
2020
private readonly ContextMenu trayMenu;
21-
readonly RegistryKey registryKey = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
2221
private readonly string appName = "WinToLinux";
2322

2423
List<string> uefi = new List<string>();
@@ -30,7 +29,6 @@ public static void Main()
3029

3130
public SysTrayApp()
3231
{
33-
bool isStart = registryKey.GetValue(appName) != null;
3432
GetMenuItems();
3533

3634
currentValue = bootsequence ?? uuid.First();
@@ -40,7 +38,7 @@ public SysTrayApp()
4038
trayMenu = new ContextMenu();
4139
trayMenu.MenuItems.Add("Settings").Enabled = false;
4240
trayMenu.MenuItems.Add("-");
43-
trayMenu.MenuItems.Add("Start with system", OnRegisterInStartup).Checked = isStart;
41+
trayMenu.MenuItems.Add("Start with system", OnRegisterInStartup).Checked = isTaskEnable();
4442
trayMenu.MenuItems.Add("-");
4543
trayMenu.MenuItems.Add("Reboot to...").Enabled = false;
4644
trayMenu.MenuItems.Add("-");
@@ -104,20 +102,52 @@ private void OnMenuClick(object sender, EventArgs e)
104102
}
105103
}
106104

107-
private void OnRegisterInStartup(object sender, EventArgs e)
105+
private void CreateTask()
108106
{
109-
bool isStartup = registryKey.GetValue(appName) == null;
107+
using (TaskService ts = new TaskService())
108+
{
109+
TaskDefinition td = ts.NewTask();
110+
111+
td.RegistrationInfo.Description = "WinToLinux. Start on boot";
112+
td.Triggers.Add(new LogonTrigger());
113+
td.Actions.Add(new ExecAction(Application.ExecutablePath, null, null));
114+
td.Principal.RunLevel = TaskRunLevel.Highest;
115+
116+
ts.RootFolder.RegisterTaskDefinition(appName, td);
117+
}
118+
}
110119

111-
if (isStartup)
120+
private void DeleteTask()
121+
{
122+
using (TaskService ts = new TaskService())
112123
{
113-
registryKey.SetValue(appName, Application.ExecutablePath);
114-
trayMenu.MenuItems[2].Checked = true;
124+
if (ts.GetTask(appName) != null)
125+
{
126+
ts.RootFolder.DeleteTask(appName);
127+
}
115128
}
116-
else
129+
}
130+
131+
private bool isTaskEnable()
132+
{
133+
using (TaskService ts = new TaskService())
134+
{
135+
return (ts.GetTask(appName) != null);
136+
}
137+
}
138+
139+
private void OnRegisterInStartup(object sender, EventArgs e)
140+
{
141+
if (isTaskEnable())
117142
{
118-
registryKey.DeleteValue(appName);
143+
DeleteTask();
119144
trayMenu.MenuItems[2].Checked = false;
120145
}
146+
else
147+
{
148+
CreateTask();
149+
trayMenu.MenuItems[2].Checked = true;
150+
}
121151
}
122152

123153
private void OnReboot(object sender, EventArgs e)

Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// Можно задать все значения или принять номера сборки и редакции по умолчанию
3333
// используя "*", как показано ниже:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.0.0.0")]
36-
[assembly: AssemblyFileVersion("1.0.0.0")]
35+
[assembly: AssemblyVersion("1.1.0")]
36+
[assembly: AssemblyFileVersion("1.1.0")]

WinToLinux.csproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<FileAlignment>512</FileAlignment>
1313
<Deterministic>true</Deterministic>
1414
<TargetFrameworkProfile />
15+
<IsWebBootstrapper>false</IsWebBootstrapper>
1516
<PublishUrl>publish\</PublishUrl>
1617
<Install>true</Install>
1718
<InstallFrom>Disk</InstallFrom>
@@ -24,7 +25,6 @@
2425
<MapFileExtensions>true</MapFileExtensions>
2526
<ApplicationRevision>0</ApplicationRevision>
2627
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
27-
<IsWebBootstrapper>false</IsWebBootstrapper>
2828
<UseApplicationTrust>false</UseApplicationTrust>
2929
<BootstrapperEnabled>true</BootstrapperEnabled>
3030
</PropertyGroup>
@@ -59,10 +59,15 @@
5959
<ApplicationIcon>WtL.ico</ApplicationIcon>
6060
</PropertyGroup>
6161
<ItemGroup>
62+
<Reference Include="Microsoft.CSharp" />
63+
<Reference Include="Microsoft.Win32.TaskScheduler, Version=2.10.1.0, Culture=neutral, PublicKeyToken=e25603a88b3aa7da, processorArchitecture=MSIL">
64+
<HintPath>packages\TaskScheduler.2.10.1\lib\net452\Microsoft.Win32.TaskScheduler.dll</HintPath>
65+
</Reference>
6266
<Reference Include="System" />
6367
<Reference Include="System.Data" />
6468
<Reference Include="System.Deployment" />
6569
<Reference Include="System.Drawing" />
70+
<Reference Include="System.IO.Compression" />
6671
<Reference Include="System.Windows.Forms" />
6772
<Reference Include="System.Xml" />
6873
</ItemGroup>
@@ -83,6 +88,7 @@
8388
</Compile>
8489
<None Include="app.config" />
8590
<None Include="app.manifest" />
91+
<None Include="packages.config" />
8692
<None Include="Properties\Settings.settings">
8793
<Generator>SettingsSingleFileGenerator</Generator>
8894
<LastGenOutput>Settings.Designer.cs</LastGenOutput>

packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="TaskScheduler" version="2.10.1" targetFramework="net48" />
4+
</packages>

0 commit comments

Comments
 (0)