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

C#WpfCompiler ErrorsClickonce

C# Problem Overview


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).

C# Solutions


Solution 1 - C#

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.


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...

Solution 2 - C#

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:

enter image description here

Solution 3 - C#

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

Solution 4 - 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.

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.

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

Hope this helps!

For .NET Core and .NET 5+

If you're stumbling upon this in the 20s, this is how you would change the above to work with .NET Core and .NET 5+

The only function that needs changing is the AdminRelauncher and it should look like this instead.

private static void AdminRelauncher()
{
    if (!IsRunAsAdmin())
    {
        ProcessStartInfo proc = new ProcessStartInfo();
        proc.UseShellExecute = true;
        proc.WorkingDirectory = Environment.CurrentDirectory;
        proc.FileName = Assembly.GetEntryAssembly().Location.Replace(".dll", ".exe");

        proc.Verb = "runas";

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

The only big changes is as someone pointed out Application isn't always available. So Environment.Exit(0) can replace it and the filename needs to replace .exe with .dll. This has been tested as of .NET 6

Solution 5 - C#

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

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

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

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

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

restruct main method like:

    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!

Solution 6 - C#

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.

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

Solution 7 - C#

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).

Solution 8 - C#

Here is the code snippet for 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.

Solution 9 - C#

For anyone who's run into this, I thought I'd contribute what ended up working for me.

Yep, the 'Enable ClickOnce security settings' option automatically gets re-checked, if you un-check it, when you do Build > Publish .

For me, I don't need to 'Publish' -- it's a simple, portable .exe that creates Scheduled Tasks for my users and I needed to make sure it elevated, even when logged-in as an Administrator.

So I just grabbed my latest .exe from \bin\Release and that's what gets deployed on my clients' systems.

Worked just as expected -- i.e. when I put it on a system w/ UAC enabled/at its highest setting, the .exe has the 'shield' on it, and when I run it, even when logged-in as an Administrator, it elevates and I get the UAC prompt.

My little task scheduler app is now able to create the task without getting an 'Access Denied' error (which previously, could only be worked-around by right-clicking the .exe and clicking Run as Administrator).

Solution 10 - C#

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

 <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:

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

Solution 11 - C#

just

Imports System.security

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLMSView Question on Stackoverflow
Solution 1 - C#LMSView Answer on Stackoverflow
Solution 2 - C#t_plusplusView Answer on Stackoverflow
Solution 3 - C#Victor RosuView Answer on Stackoverflow
Solution 4 - C#Tyler CView Answer on Stackoverflow
Solution 5 - C#malajisiView Answer on Stackoverflow
Solution 6 - C#CodeBreakerView Answer on Stackoverflow
Solution 7 - C#Bhagwat KView Answer on Stackoverflow
Solution 8 - C#TzwenniView Answer on Stackoverflow
Solution 9 - C#SamskaraView Answer on Stackoverflow
Solution 10 - C#BrianView Answer on Stackoverflow
Solution 11 - C#AdityaView Answer on Stackoverflow