1- using Microsoft . Win32 ;
1+ using Microsoft . Win32 . TaskScheduler ;
22using System ;
33using System . Collections . Generic ;
44using 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 )
0 commit comments