c# - 拉皮条我的 UAC 和一些关于它的问题

标签 c# .net windows-7 windows-vista uac

我有这个应用程序需要在 protected 路径(如 %PROGRAMFILES%)中做一些事情,我知道我应该使用 %APPDATA%,但我现在不能改变它。我已经隔离了所有可能需要 UAC 才能出现在另一个项目上的东西,这是一个示例代码:

using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;

class Class1
{
    static void Main(string[] args)
    {
        try
        {
            File.CreateText(Path.Combine(Application.StartupPath, "something.txt"));
        }
        catch (UnauthorizedAccessException ex)
        {
            MessageBox.Show(ex.Message, "UnauthorizedAccessException", MessageBoxButtons.OK, MessageBoxIcon.Error);

            if (args.Length == 0)
            {
                Process proc = new Process();
                proc.StartInfo.FileName = Application.ExecutablePath;
                proc.StartInfo.Arguments = "not again";
                proc.StartInfo.Verb = "runas";
                proc.Start();
            }
            else
            {
                MessageBox.Show("Exit to avoid loop.");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
}

因此,我从我的主程序中调用这个可执行文件,如果它由于未经授权的访问而失败,它将自行启动并显示 UAC 请求。

我的问题是:

1) 我不得不将项目输出从 DLL 转换为 EXE,因为我找不到任何方法从 DLL 请求 UAC 提升,有没有简单的方法可以做到这一点?

2) 我还注意到一些程序显示个性化的 UAC 消息,带有程序 Logo 和所有这些东西,让我举个例子:

Ugly UAC

Personalized UAC

我怎样才能为我的程序做到这一点?

3) 为了避免在以提升的权限运行时进入循环,它得到另一个 UnauthorizedAccessException 我做了那个传递任何参数的事情。为了实现相同的目标,您会怎么做?

我想这就是全部了。感谢您的宝贵时间。

最佳答案

我遇到了同样的问题。谷歌搜索大约 2 天后,我找到了满足我需求的唯一解决方案——以管理权限启动应用程序。我启动应用程序,检查它是否以管理员身份运行。如果没有 - 使用管理权限重新启动它。

    static void Main(string[] args)
    {
        if (NeedElevation(args) && Elevate(args))
        { // If elevastion succeeded then quit.
            return;
        }
        // your code here
    }

    public static bool Elevate(string[] args)
    {
        try
        {
            ProcessStartInfo info = Process.GetCurrentProcess().StartInfo;
            info.Verb = "runas";
            info.Arguments = NoElevateArgument;
            foreach (string arg in args)
            {
                info.Arguments += ' ' + arg;
            }
            info.FileName = Assembly.GetEntryAssembly().Location;

            Process process = new System.Diagnostics.Process();
            process.StartInfo = info;

            process.Start();
        }
        catch (Exception)
        {
            MessageBox.Show("You don't have administrative privileges thus the Automatic Application Updates cannot be started. But the rest of application is available as usually.",
                "Not enough user rights", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return false;
        }

        return true;
    }

关于c# - 拉皮条我的 UAC 和一些关于它的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1631444/

相关文章:

c# - 用 C++ 开发 Win32 应用程序比用 C# 开发 .NET 应用程序有什么优势?

c# - 使用自定义 logentry 类和自定义 logformatter

c# - 在进行 Web API 调用时捕获异常

c# - 阅读推特提要

postgresql - windows 7下如何设置postgres用户

c# - 自定义 CSS 标签

c# - 为什么预处理器在 C/C++/ObjC 以外的语言中不太常见?

.net - 在 asp.net 应用程序 View 中固定元素位置

.net - 用于触发墙纸随机播放的 Windows API

java - 如何允许使用 Java 或 SWT 将文件拖放到 Windows 7 任务栏图标上?