Windows 具有管理员权限的表单启动
Windows Form Startup with Admin Permission
我有一个 windows 表单应用程序需要 运行 的管理员权限,为此,我使用此代码:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
完成开发的下一步是此 windows 表单应用程序在 windows 重新启动、关闭并再次打开或用户登录后启动。
这是我的问题,这个应用程序需要管理员权限,并且需要在系统启动后启动,但我不知道该怎么做。
我做了什么:
将应用程序的可执行路径放到regedit
Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
我确实创建了 Windows 服务项目
这些选项不起作用,有人可以帮助我吗?
谢谢。
我找到 运行 一个在启动时具有管理员权限的应用程序的答案。
基本上我刚刚创建了一个运行级别最高的任务,您的触发器已登录。
我在这个存储库的 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();
//deleteTask();
}
static void addTask()
{
// Get the service on the local machine
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
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;
// Register the task in the root folder
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 作为管理员,或者将此代码放入程序的安装过程中
让我知道这是否对某人有用
我有一个 windows 表单应用程序需要 运行 的管理员权限,为此,我使用此代码:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
完成开发的下一步是此 windows 表单应用程序在 windows 重新启动、关闭并再次打开或用户登录后启动。
这是我的问题,这个应用程序需要管理员权限,并且需要在系统启动后启动,但我不知道该怎么做。
我做了什么:
将应用程序的可执行路径放到regedit
Registry.CurrentUser.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
我确实创建了 Windows 服务项目
这些选项不起作用,有人可以帮助我吗?
谢谢。
我找到 运行 一个在启动时具有管理员权限的应用程序的答案。
基本上我刚刚创建了一个运行级别最高的任务,您的触发器已登录。
我在这个存储库的 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();
//deleteTask();
}
static void addTask()
{
// Get the service on the local machine
using (TaskService ts = new TaskService())
{
// Create a new task definition and assign properties
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;
// Register the task in the root folder
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 作为管理员,或者将此代码放入程序的安装过程中
让我知道这是否对某人有用