104

所以我正在编写一个需要访问注册表的应用程序。我没有触及任何构建设置,希望在我添加其他内容(例如描述或名称)之前让它工作。

出乎意料的是,我得到一个不会消失的错误。ClickOnce does not support the request execution level 'requireAdministrator'.现在,我还没有在这个应用程序中接触过 ClickOnce。我所做的只是包含一个请求这些权限的清单文件。

我现在的问题是这个错误不会消失,我无法编译我的程序。关于做什么的任何建议?(旁注:我要睡觉了,所以明天下午我会检查这个)。

4

11 回答 11

158

编辑:这条评论也给出了一个很好的答案。

每当您单击“发布”时,单击一次似乎会启用,无论您是否愿意!如果您使用的是“requireAdministrator”,那么您似乎无法使用 ClickOnce,因此无法“发布”您的项目。


原来的:

原来,在“安全”选项卡下,“启用 ClickOnce 安全设置”已被选中。尽管我没有检查它。无论如何,取消选中停止 ClickOnce 给我的错误。找了好久才发现...

于 2012-06-14T15:59:06.897 回答
50

我知道这是一个老问题,但两年后我来到这里,所以:

您可以从项目属性的安全选项卡中禁用 ClicKOnce 以帮助解决问题;见下文:

在此处输入图像描述

于 2015-03-17T17:23:49.743 回答
20

如果您曾经使用过发布向导或“立即发布”,则单击一次复选框会自动选中...

于 2013-10-03T05:53:36.300 回答
19

我知道这很旧,但我偶然发现它正在寻找答案。就我而言,我正在使用发布功能,我需要继续使用它。我还需要访问管理功能。因此,出于这个原因,上述答案都不适合我。

我最终在我的应用程序的最开始添加了一个方法,该方法检查它是否以管理员身份运行,如果不是,则以管理员身份重新启动。为此,您需要添加以下参考。

using System;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;

然后你需要把它放在你的主要方法可以方便地访问的地方。我正在使用 WPF,所以我将它添加到 MainWindow.xaml.cs 但您可以在代码的早期任何地方添加它。如果需要,请记住在这些方法中添加“静态”。

private void AdminRelauncher()
{
    if (!IsRunAsAdmin())
    {
        ProcessStartInfo proc = new ProcessStartInfo();
        proc.UseShellExecute = true;
        proc.WorkingDirectory = Environment.CurrentDirectory;
        proc.FileName = Assembly.GetEntryAssembly().CodeBase;

        proc.Verb = "runas";

        try
        {
            Process.Start(proc);
            Application.Current.Shutdown();
        }
        catch(Exception ex)
        {
            Console.WriteLine("This program must be run as an administrator! \n\n" + ex.ToString());
        }
    }
}

private bool IsRunAsAdmin()
{
    try
    {
        WindowsIdentity id = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(id);
        return principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    catch (Exception)
    {
        return false;
    }
}

最后,在程序开始时,添加对该方法的引用。就我而言,我将它添加到 MainWindow 但也将其添加到 Main 作品。

public MainWindow()
{
    InitializeComponent();
    AdminRelauncher(); //This is the only important line here, add it to a place it gets run early on.
}

希望这可以帮助!

于 2017-09-06T16:19:59.143 回答
6

对于那些使用取消选中“启用 ClickOnce 安全设置”无法工作的人,请尝试我找到的方法。

首先,将您的 app.manifest requestedExecutionLevel 项保持原样:

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

然后像这样编辑 Program.cs 文件:

using System;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;
using System.Windows.Forms;

重构主要方法,如:

    static void Main()
        {
            var wi = WindowsIdentity.GetCurrent();
            var wp = new WindowsPrincipal(wi);

            bool runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator);

            if (!runAsAdmin)
            {
                // It is not possible to launch a ClickOnce app as administrator directly,
                // so instead we launch the app as administrator in a new process.
                var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase);

                // The following properties run the new process as administrator
                processInfo.UseShellExecute = true;
                processInfo.Verb = "runas";

                // Start the new process
                try
                {
                    Process.Start(processInfo);
                }
                catch (Exception)
                {
                    // The user did not allow the application to run as administrator
                    MessageBox.Show("Sorry, but I don't seem to be able to start " + 
                       "this program with administrator rights!");
                }

                // Shut down the current process
                Application.Exit();
            }
            else
            {
                // We are running as administrator
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
        }

它适用于 Windows 10 和 Visual Studio 2019!

于 2019-12-04T02:24:22.337 回答
4

可以通过选择“启用 ClickOnce 安全设置”(因为在发布期间不能“取消选中”,如前所述)然后选择“这是部分信任的应用程序”来实现此操作。下拉菜单中会自动选择“本地 Intranet”,非常好。

保存您的更改,发布应用程序,完成滑雪。:-)安全设置片段

于 2017-09-27T20:01:43.553 回答
2

我有同样的问题我通过取消选中“启用 ClickOnce 安全设置”来解决它 要在 Visual Studio 中找到此选项右键单击您的项目 ==>properties==>选择安全==>启用 ClickOnce 安全设置(此选项是已经检查过,所以我取消了检查,我的问题得到了解决)。

于 2014-08-28T17:34:41.213 回答
2

这是 VB.NET 的代码片段

If Not New WindowsPrincipal(WindowsIdentity.GetCurrent).IsInRole(WindowsBuiltInRole.Administrator) Then
            Process.Start(New ProcessStartInfo With { _
                                                     .UseShellExecute = True, _
                                                     .WorkingDirectory = Environment.CurrentDirectory, _
                                                     .FileName = Assembly.GetEntryAssembly.CodeBase, _
                                                     .Verb = "runas"})

编辑:但是如果你以这种方式部署,一些 AV 软件会阻止你的代码。

于 2017-10-03T13:57:00.807 回答
1

对于遇到这种情况的任何人,我想我会贡献最终对我有用的东西。

是的,“启用 ClickOnce 安全设置”选项会在您执行Build > Publish时自动重新检查,如果您取消选中它。

对我来说,我不需要“发布”——它是一个简单、可移植的 .exe,它为我的用户创建计划任务,我需要确保它提升,即使以管理员身份登录也是如此。

所以我刚刚从\bin\Release中获取了我最新的 .exe ,这就是我在客户系统上部署的内容。

正如预期的那样工作 - 即当我将它放在启用 UAC/在其最高设置的系统上时,.exe 上面有“盾牌”,当我运行它时,即使以管理员身份登录,它升高了,我得到了 UAC 提示。

我的小任务计划程序应用程序现在能够创建任务而不会出现“拒绝访问”错误(以前只能通过右键单击 .exe 并单击以管理员身份运行来解决此问题)。

于 2020-06-09T21:58:22.190 回答
0

查看您的 app.Manifest 文件,您会看到:

 <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />

评论中有说明,但只需删除“requireAdministrator”并将其插入就可以解决我的问题:

 <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
于 2014-04-16T12:24:36.660 回答
-13

只是

Imports System.security

你不会得到任何错误,你的应用程序将以管理员身份运行

于 2015-06-19T08:49:04.267 回答