正确的方法来处理UAC在C# [英] Correct way to deal with UAC in C#

查看:948
本文介绍了正确的方法来处理UAC在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有被安装到在Program Files文件夹目录中的应用程序(Windows服务)。除了这个应用程序是用来配置服务(除其他事项外)的另一个WinForms应用程序。当它的配置,这样可以节省改变,生活旁边的服务配置文件。

I have an application (Windows service) that is installed into a directory in the Program Files folder. Alongside this application is another WinForms application that is used to configure the service (amongst other things). When it does configuration, it saves changes to a config file that lives alongside the service.

在Vista / Win7的运行时,UAC prevents从保存到配置文件的用户。我希望做的是:

When running on Vista/Win7, UAC prevents the user from saving to the config file. What I would like to do is:

  • 把盾牌图标旁边,用配置的菜单项
  • 该项目选择提示UAC权限
  • 只显示图标/提示,当需要它的OS上
  • 只显示图标/提示时,权限是必需的(例如,如果应用程序被安装的地方,不需要UAC权限)

我真的不希望运行的整个应用程序作为管理员,因为它也被用于其他用途,不需要UAC权限(这样设置的应用程序清单文件是不是正确的解决方案)。我也假设(纠正我,如果我错了),一旦UAC权限已被批准,我现有的过程中不能执行操作,并且我将要开始一个新的进程。

I don't really want to run the whole application as an administrator, as it is also used for other purposes that do not require UAC permissions (so setting an application manifest file is not the correct solution). I'm also assuming (correct me if I'm wrong) that once UAC permissions have been granted, my existing process cannot perform the action and that I will need to start a new process.

我怎样才能最好地实现这一点?

How can I best achieve this?

推荐答案

这是相当容易的。将代替菜单项的盾牌图标上保存更改配置文件按钮。在此之前,没有请求UAC权限,直到最后一刻的Windows操作。按钮实际上将再次启动您的可执行文件作为管理员提供一个特殊的命令行(你决定)执行配置文件保存。使用命名管道(一定要给予正确的权限)的配置数据传递给你的第二个实例,如果你不想使用命令行进行数据传递。

This is fairly easy. Put a shield icon on the button that saves changes to the configuration file, instead of the menu item. This follows the Windows behavior of not requesting UAC permissions until the last moment. The button actually will launch your executable again as administrator with a special command line (that you decide) to perform the configuration file saving. Use a named pipe (be sure to give it the correct permissions) to pass the configuration data to your second instance if your don't want to use the command line for data passing.

有关启动您的可执行文件:

For launching your executable:

ProcessStartInfo info = new ProcessStartInfo();
info.FileName = "YOUR EXE";
info.UseShellExecute = true;
info.Verb = "runas"; // Provides Run as Administrator
info.Arguments = "YOUR SPECIAL COMMAND LINE";

if (Process.Start(info) != null)
{ 
    // The user accepted the UAC prompt.
}

这个作品也当UAC不存在(Windows XP中),因为它只会以管理员身份如果可能的话运行,或提示凭据。您可以检查操作系统是否需要UAC通过简单地做 Environment.OSVersion.Version.Major == 6 。 6为Windows Vista和7可以确保您使用的是Windows通过查看 Environment.OSVersion.Platform

This works also when UAC doesn't exist (Windows XP), because it will simply run as administrator if possible, or prompt for credentials. You can check whether the OS requires UAC by simply doing Environment.OSVersion.Version.Major == 6. 6 is both Windows Vista and 7. You can make sure you're using Windows by looking at Environment.OSVersion.Platform.

有关检测无论你的应用程序已经是管理员,你可以这样做:

For detecting whether you're application is already admin, you can do this:

public static bool IsAdministrator()
{
    WindowsIdentity identity = WindowsIdentity.GetCurrent();

    if (identity != null)
    {
        WindowsPrincipal principal = new WindowsPrincipal(identity);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }

    return false;
}

这篇关于正确的方法来处理UAC在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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