C# “ClickOnce 不支持请求执行级别‘requireAdministrator’。”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11023998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:09:55  来源:igfitidea点击:

"ClickOnce does not support the request execution level 'requireAdministrator.'"

c#wpfcompiler-errorsclickonce

提问by LMS

So I was writing an application that requires access to the registry. I had not touched any build settings, wanting to get the thing working before I added the other touches, such as a description or name.

Out of the blue, I get an error that will not go away. ClickOnce does not support the request execution level 'requireAdministrator'.Now, I hadn't touched ClickOnce in this application. All I had done was include a manifest file requesting these permissions.

My problem now is that this error will not go away, and I cannot compile my program. Any advice on what to do? (Side note: I am about to go to bed, so I will check this tomorrow afternoon).

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

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

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

采纳答案by LMS

Edit:This comment gives a good answer, too.

编辑:此评论也给出了很好的答案。

Click once appears to get enabled whenever you click "Publish", whether you want it to or not! If you are using "requireAdministrator" then it appears that you cannot use ClickOnce, and therefore cannot "Publish" your project.

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



Original:

原来的:

Turns out that under the Security tab, "Enable ClickOnce security settings" was checked. Even though I didn't check it. Anyway, unchecking that stopped ClickOnce giving me errors. That took a while to find...

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

回答by Victor Rosu

If you ever use the publishing wizard, or 'Publish Now', the click-once checkbox gets automatically selected...

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

回答by Brian

Take a look in your app.Manifest file and you'll see this:

看看你的 app.Manifest 文件,你会看到:

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

There's instructions there in the comments, but just deleting the "requireAdministrator" and insert this in is place solved the problem for me:

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

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

回答by Ragnar

I have the same problem s I resolve it by unchecking the "Enable ClickOnce security settings"To Find this option in Visual Studio Right Click on your Project ==>properties==>Select Security==> Enable ClickOnce security settings(This option was already checked so I unchecked it and my problem get resolved).

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

回答by t_plusplus

I know this an old question but I came here two years later so:

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

You can disable the ClicKOnce from the Security tab on project properites to help the issue; see below:

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

enter image description here

在此处输入图片说明

回答by Aditya

just

只是

Imports System.security

and U will get no error and your application will be run as admin

你不会出错,你的应用程序将以管理员身份运行

回答by Tyler C

I know this is old but I stumbled across it looking for answers. In my case, I AM using the publish function and I need to keep using it. I also need access to admin capabilities. So for that reason, none of the above answers worked for me.

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

I ended up adding a method to the very start of my application that checks if it's being run as an administrator and if it isn't, relaunch itself as an admin. To do this, you need the following references added.

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

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

Then you will need to put this somewhere that your main method has handy access to. I'm using WPF so I added it to MainWindow.xaml.cs but you can add it anywhere early on in your code. Just remember to add "static" to these methods should you need it.

然后你需要把它放在你的主要方法可以方便地访问的地方。我正在使用 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;
    }
}

Lastly, at the start of your program, add a reference to the method. In my case, I added it to MainWindow but adding it to Main works too.

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

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

Hope this helps!

希望这可以帮助!

回答by CodeBreaker

This action can be achieved by selecting "Enable ClickOnce security settings" (since it cannot be "unchecked" during a Publish, as stated) and then by selecting "This is a partial trust application". "Local Intranet" will be automatically selected in the drop-down menu which is perfectly fine.

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

Save your changes, Publish the application, done-skis. :-)Security Settings Snippet

保存更改,发布应用程序,done-skis。:-)安全设置片段

回答by Tzwenni

Here is the code snippet for VB.NET

这是 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"})

EDIT: But if you deploy in this way, some AV-Software blocks your code.

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

回答by malajisi

For those who use uncheck "Enable ClickOnce security settings" can't work, to try the method I find.

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

First, leave your app.manifest requestedExecutionLevel item as is:

首先,让你的 app.manifest 请求执行级别项目保持原样:

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

And then you edit your Program.cs file like this:

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

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

restruct main method like:

重构 main 方法,如:

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

It works on Windows 10 and Visual Studio 2019!

它适用于 Windows 10 和 Visual Studio 2019!