给应用程序提升 UAC [英] Giving application elevated UAC

查看:28
本文介绍了给应用程序提升 UAC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要 UAC 提升的应用程序.

I have an application which needs the UAC elevation.

我有代码可以让我提供,但应用程序打开两次..这是问题..

I have the code which lets me give that but the application opens twice.. which is the issue..

这里是Form1中的代码:

so here is the code in Form1:

 public Form1()
    {
        InitializeComponent();

        WindowsPrincipal pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
        bool hasAdministrativeRight = pricipal.IsInRole(WindowsBuiltInRole.Administrator);           

        if (!hasAdministrativeRight)
        {
            ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.UseShellExecute = true;
            startInfo.WorkingDirectory = Environment.CurrentDirectory;
            startInfo.FileName = Application.ExecutablePath;
            startInfo.Verb = "runas";
            try
            {
                Process p = Process.Start(startInfo);
            }
            catch (System.ComponentModel.Win32Exception ex)
            {
                return;
            }

        }

    }

这是代码programs.cs

and this is the code programs.cs

       static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }

在调试时我发现它首先执行

on debugging i find out that first it executes

进程 p = Process.Start(startInfo);

Process p = Process.Start(startInfo);

打开应用程序 UAC 提升对话框,然后打开应用程序

which opens the application UAC elevation dialog and then opens the application

然后它转到

Application.Run(new Form1());

Application.Run(new Form1());

在 main() 中并再次打开应用程序.

in main() and opens the application again.

我不想它再次打开应用程序...

i dont want it to open the app again...

我是新手,我做错了什么,我是否需要在打开 UAC 后关闭它..

i am new to this is there anything i am doing wrong and do i need to close the UAC once its open..

谢谢

推荐答案

您无需干预所有这些以确保您的应用程序始终以提升的权限运行.您可以简单地添加应用程序清单,指示 Windows 运行您的应用程序, 无需编写一行代码即可出现 UAC 提示.

You don't need to meddle with all that to make sure that your application always runs with elevated privileges. You can simply add an application manifest which instructs Windows to run your app elevated, and the UAC prompt will appear without you needing to write a single line of code.

有一个相关问题的答案也描述了如何在此处添加清单:How can I embed an application manifest into an application使用 VS2008?

There's a related question with an answer that also describes how to add a manifest here: How can I embed an application manifest into an application using VS2008?

这篇关于给应用程序提升 UAC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆