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

时间:2017-12-27 13:42:35

标签: c# .net windows windows-forms-designer uac

我有一个需要管理员权限才能运行的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 个答案:

答案 0 :(得分:0)

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

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

我在此存储库中的 vb 中找到了代码:https://bitbucket.org/trparky/start_program_at_startup_without_uac

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);
                }
            }
        }
    }
}

所以我所做的就是将这段代码翻译成c#并进行测试

{{1}}

我在示例中使用 regedit.exe ,因为此程序需要管理员权限才能运行。

创建任务,进行注销并再次登录,登录后您将看到注册表。

OBS:要创建或删除任务,您已经以管理员身份运行visual studio,或将此代码放入程序的安装过程中

让我知道这是否适用于某人

相关问题