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

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

问题描述

我有需要UAC提升的应用程序。

I have an application which needs the UAC elevation.

我有code,它可以让我给那个,但应用程序打开两次..这是问题。

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

所以这里是在Form1上code:

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

        }

    }

这就是code 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(新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提示会出现没有你需要写code一行。

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.

有同也介绍了如何添加一个清单在这里回答一个相关的问题:我怎么可以嵌入应用程序清单到应用程序使用VS​​2008?

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天全站免登陆