Skip to content

Commit

Permalink
Move from registry startup option to create a task. Close Program not…
Browse files Browse the repository at this point in the history
… auto starting. (Windows 11) #2
  • Loading branch information
Toxblh committed Jan 16, 2023
1 parent 2403588 commit 15b38fe
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
52 changes: 41 additions & 11 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Microsoft.Win32;
using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Diagnostics;
Expand All @@ -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<string> uefi = new List<string>();
Expand All @@ -30,7 +29,6 @@ public static void Main()

public SysTrayApp()
{
bool isStart = registryKey.GetValue(appName) != null;
GetMenuItems();

currentValue = bootsequence ?? uuid.First();
Expand All @@ -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("-");
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
8 changes: 7 additions & 1 deletion WinToLinux.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
Expand All @@ -24,7 +25,6 @@
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
Expand Down Expand Up @@ -59,10 +59,15 @@
<ApplicationIcon>WtL.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Microsoft.Win32.TaskScheduler, Version=2.10.1.0, Culture=neutral, PublicKeyToken=e25603a88b3aa7da, processorArchitecture=MSIL">
<HintPath>packages\TaskScheduler.2.10.1\lib\net452\Microsoft.Win32.TaskScheduler.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Deployment" />
<Reference Include="System.Drawing" />
<Reference Include="System.IO.Compression" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
</ItemGroup>
Expand All @@ -83,6 +88,7 @@
</Compile>
<None Include="app.config" />
<None Include="app.manifest" />
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
4 changes: 4 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="TaskScheduler" version="2.10.1" targetFramework="net48" />
</packages>

0 comments on commit 15b38fe

Please sign in to comment.