我找到了在启动时使用管理员权限运行应用程序的答案。
基本上,我只是创建了一个具有运行级别的任务
最高
您的触发器在登录时。
我在中找到了代码
vb
在此存储库中:
https://bitbucket.org/trparky/start_program_at_startup_without_uac
Sub addTask(taskName As String, taskDescription As String, taskEXEPath As String, taskParameters As String)
taskName = taskName.Trim
taskDescription = taskDescription.Trim
taskEXEPath = taskEXEPath.Trim
taskParameters = taskParameters.Trim
If Not IO.File.Exists(taskEXEPath) Then
MsgBox("Executable path not found.", MsgBoxStyle.Critical, Me.Text)
Exit Sub
End If
Dim taskService As TaskService = New TaskService()
Dim newTask As TaskDefinition = taskService.NewTask
newTask.RegistrationInfo.Description = taskDescription
If chkEnabled.Checked Then newTask.Triggers.Add(New LogonTrigger)
Dim exeFileInfo As New FileInfo(taskEXEPath)
newTask.Actions.Add(New ExecAction(Chr(34) & taskEXEPath & Chr(34), taskParameters, exeFileInfo.DirectoryName))
newTask.Principal.RunLevel = TaskRunLevel.Highest
newTask.Settings.Compatibility = TaskCompatibility.V2_1
newTask.Settings.AllowDemandStart = True
newTask.Settings.DisallowStartIfOnBatteries = False
newTask.Settings.RunOnlyIfIdle = False
newTask.Settings.StopIfGoingOnBatteries = False
newTask.Settings.AllowHardTerminate = False
newTask.Settings.UseUnifiedSchedulingEngine = True
newTask.Settings.ExecutionTimeLimit = Nothing
newTask.Settings.Priority = ProcessPriorityClass.Normal
newTask.Principal.LogonType = TaskLogonType.InteractiveToken
taskService.RootFolder.SubFolders(strTaskFolderName).RegisterTaskDefinition(taskName, newTask)
newTask.Dispose()
taskService.Dispose()
newTask = Nothing
taskService = Nothing
End Sub
所以我所做的就是把这段代码翻译成c语言并进行测试
using Microsoft.Win32.TaskScheduler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CreateTaskTest
{
class Program
{
static void Main(string[] args)
{
addTask();
}
static void addTask()
{
using (TaskService ts = new TaskService())
{
TaskDefinition newTask = ts.NewTask();
newTask.RegistrationInfo.Description = "Rondinelli Morais Create Task";
newTask.Triggers.Add(new LogonTrigger());
newTask.Actions.Add(new ExecAction("C:\\Windows\\regedit.exe"));
newTask.Principal.RunLevel = TaskRunLevel.Highest;
newTask.Principal.LogonType = TaskLogonType.InteractiveToken;
newTask.Settings.Compatibility = TaskCompatibility.V2_1;
newTask.Settings.AllowDemandStart = true;
newTask.Settings.DisallowStartIfOnBatteries = false;
newTask.Settings.RunOnlyIfIdle = false;
newTask.Settings.StopIfGoingOnBatteries = false;
newTask.Settings.AllowHardTerminate = false;
newTask.Settings.UseUnifiedSchedulingEngine = true;
newTask.Settings.Priority = System.Diagnostics.ProcessPriorityClass.Normal;
ts.RootFolder.RegisterTaskDefinition(@"Test", newTask);
newTask.Dispose();
ts.Dispose();
}
}
static void deleteTask()
{
using (TaskService ts = new TaskService())
{
var tasks = ts.FindAllTasks(new System.Text.RegularExpressions.Regex(@"Test"));
foreach(var task in tasks){
ts.RootFolder.DeleteTask(task.Name);
}
}
}
}
}
我正在使用
regedit。exe文件
在示例上,因为此程序需要管理员权限才能运行。
创建任务,注销并再次登录,登录后您将看到regedit打开。
OBS:要创建或删除您以管理员身份运行visual studio的任务,或将此代码放在程序的安装过程中
如果这对某人有用,请告诉我