所以我正在编写一个需要访问注册表的应用程序。我没有触及任何构建设置,希望在我添加其他内容(例如描述或名称)之前让它工作。
出乎意料的是,我得到一个不会消失的错误。ClickOnce does not support the request execution level 'requireAdministrator'.
现在,我还没有在这个应用程序中接触过 ClickOnce。我所做的只是包含一个请求这些权限的清单文件。
我现在的问题是这个错误不会消失,我无法编译我的程序。关于做什么的任何建议?(旁注:我要睡觉了,所以明天下午我会检查这个)。
11 回答
编辑:这条评论也给出了一个很好的答案。
每当您单击“发布”时,单击一次似乎会启用,无论您是否愿意!如果您使用的是“requireAdministrator”,那么您似乎无法使用 ClickOnce,因此无法“发布”您的项目。
原来的:
原来,在“安全”选项卡下,“启用 ClickOnce 安全设置”已被选中。尽管我没有检查它。无论如何,取消选中停止 ClickOnce 给我的错误。找了好久才发现...
我知道这是一个老问题,但两年后我来到这里,所以:
您可以从项目属性的安全选项卡中禁用 ClicKOnce 以帮助解决问题;见下文:
如果您曾经使用过发布向导或“立即发布”,则单击一次复选框会自动选中...
我知道这很旧,但我偶然发现它正在寻找答案。就我而言,我正在使用发布功能,我需要继续使用它。我还需要访问管理功能。因此,出于这个原因,上述答案都不适合我。
我最终在我的应用程序的最开始添加了一个方法,该方法检查它是否以管理员身份运行,如果不是,则以管理员身份重新启动。为此,您需要添加以下参考。
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.
}
希望这可以帮助!
对于那些使用取消选中“启用 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!
我有同样的问题我通过取消选中“启用 ClickOnce 安全设置”来解决它 要在 Visual Studio 中找到此选项右键单击您的项目 ==>properties==>选择安全==>启用 ClickOnce 安全设置(此选项是已经检查过,所以我取消了检查,我的问题得到了解决)。
这是 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 软件会阻止你的代码。
对于遇到这种情况的任何人,我想我会贡献最终对我有用的东西。
是的,“启用 ClickOnce 安全设置”选项会在您执行Build > Publish时自动重新检查,如果您取消选中它。
对我来说,我不需要“发布”——它是一个简单、可移植的 .exe,它为我的用户创建计划任务,我需要确保它提升,即使以管理员身份登录也是如此。
所以我刚刚从\bin\Release中获取了我最新的 .exe ,这就是我在客户系统上部署的内容。
正如预期的那样工作 - 即当我将它放在启用 UAC/在其最高设置的系统上时,.exe 上面有“盾牌”,当我运行它时,即使以管理员身份登录,它升高了,我得到了 UAC 提示。
我的小任务计划程序应用程序现在能够创建任务而不会出现“拒绝访问”错误(以前只能通过右键单击 .exe 并单击以管理员身份运行来解决此问题)。
查看您的 app.Manifest 文件,您会看到:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
评论中有说明,但只需删除“requireAdministrator”并将其插入就可以解决我的问题:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
只是
Imports System.security
你不会得到任何错误,你的应用程序将以管理员身份运行