代码之家  ›  专栏  ›  技术社区  ›  Rondinelli Morais

具有管理员权限的Windows窗体启动

  •  -2
  • Rondinelli Morais  · 技术社区  · 7 年前

    我有一个需要管理员权限才能运行的windows窗体应用程序,为此,我使用以下代码:

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
    

    完成开发的下一步是在windows重新启动、关闭并再次打开或用户登录后启动此windows窗体应用程序。

    这是我的问题,这个应用程序需要管理员权限,需要在系统启动后启动,但我不知道该怎么做。

    我所做的事情:

    将应用程序可执行路径置于regedit上

    Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    

    我确实创建了Windows服务项目

    https://docs.microsoft.com/en-us/dotnet/framework/windows-services/walkthrough-creating-a-windows-service-application-in-the-component-designer

    这些选项不起作用,有人能帮我吗?

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Rondinelli Morais    7 年前

    我找到了在启动时使用管理员权限运行应用程序的答案。

    基本上,我只是创建了一个具有运行级别的任务 最高 您的触发器在登录时。

    我在中找到了代码 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的任务,或将此代码放在程序的安装过程中

    如果这对某人有用,请告诉我