-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use TaskScheduler instead of registering an exe path to Windows registry to run app as administrator
- Loading branch information
Showing
6 changed files
with
67 additions
and
45 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 |
---|---|---|
@@ -1,15 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<configuration> | ||
<configSections> | ||
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > | ||
<section name="UsbipdGui.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> | ||
</sectionGroup> | ||
</configSections> | ||
<userSettings> | ||
<UsbipdGui.Properties.Settings> | ||
<setting name="RunAtStartup" serializeAs="String"> | ||
<value>False</value> | ||
</setting> | ||
</UsbipdGui.Properties.Settings> | ||
</userSettings> | ||
</configuration> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,51 @@ | ||
using Microsoft.Win32.TaskScheduler; | ||
using System; | ||
using System.Diagnostics; | ||
|
||
namespace UsbipdGui | ||
{ | ||
internal static class StartupSettings | ||
{ | ||
public static bool IsRegistered(in string appName) | ||
{ | ||
using TaskService taskService = new(); | ||
return taskService.GetTask(appName) is not null; | ||
} | ||
|
||
public static void RegisterTask(in string appName, in string exePath) | ||
{ | ||
try | ||
{ | ||
using TaskService taskService = new(); | ||
if (taskService.GetTask(appName) is not null) | ||
{ | ||
// already registered | ||
return; | ||
} | ||
TaskDefinition taskDefinition = taskService.NewTask(); | ||
taskDefinition.RegistrationInfo.Description = appName; | ||
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest; // Run as administrator | ||
taskDefinition.Triggers.Add(new LogonTrigger()); // Run at startup | ||
taskDefinition.Actions.Add(new ExecAction(exePath)); | ||
taskService.RootFolder.RegisterTaskDefinition(appName, taskDefinition); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine($"Catch exception {ex}"); | ||
} | ||
} | ||
|
||
public static void RemoveTask(in string appName) | ||
{ | ||
try | ||
{ | ||
using TaskService taskService = new(); | ||
taskService.RootFolder.DeleteTask(appName); | ||
} | ||
catch (Exception ex) | ||
{ | ||
Debug.WriteLine($"Catch exception {ex}"); | ||
} | ||
} | ||
} | ||
} |
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