我正在编写一个需要访问注册表的应用程序。在添加其他功能之前,比如描述或名称,我没有触碰任何构建设置,只是想让它能工作。
突然出现了一个错误,但我没有在这个应用程序中使用 ClickOnce。我所做的就是包含一个请求这些权限的清单文件。ClickOnce不支持请求执行级别为'requireAdministrator'的操作。
我的问题是这个错误无法消失,我无法编译我的程序。有什么建议吗?(附注:我即将上床睡觉,明天下午再查看此事)。
我正在编写一个需要访问注册表的应用程序。在添加其他功能之前,比如描述或名称,我没有触碰任何构建设置,只是想让它能工作。
突然出现了一个错误,但我没有在这个应用程序中使用 ClickOnce。我所做的就是包含一个请求这些权限的清单文件。ClickOnce不支持请求执行级别为'requireAdministrator'的操作。
我的问题是这个错误无法消失,我无法编译我的程序。有什么建议吗?(附注:我即将上床睡觉,明天下午再查看此事)。
编辑: 这条评论也提供了一个很好的答案。
每当您单击“发布”时,似乎都会启用Click once,无论您是否想要启用它! 如果您正在使用“requireAdministrator”,则似乎无法使用ClickOnce,因此无法“发布”您的项目。
原始内容:
事实证明,在“安全性”选项卡下,“启用ClickOnce安全性设置”被勾选了。 尽管我没有勾选它。 无论如何,取消该复选框后就可以停止ClickOnce给我带来的错误。 这花费了一些时间才找到……
我知道这是一个旧问题,但我两年后来到这里:
您可以从项目属性的安全选项卡中禁用ClicKOnce以帮助解决问题;请参见下面的截图:
我知道这可能有点老了,但我在寻找答案时偶然发现了它。在我的情况下,我正在使用发布功能,并且需要继续使用它。我还需要访问管理员权限。因此,出于这个原因,以上任何答案都对我无效。
最终,我在应用程序的开头添加了一个方法,检查它是否作为管理员运行,如果不是,则以管理员身份重新启动自己。要实现此目的,您需要添加以下引用。
using System;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;
那么,您需要将此放置在主方法方便访问的地方。我正在使用WPF,因此我将其添加到MainWindow.xaml.cs中,但您可以在代码的早期任何地方添加它。只需记住,如果需要,在这些方法中添加“static”关键字。
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.
}
希望这能帮到您!
对于.NET Core和.NET 5+
如果您在20年代遇到了这个问题,那么以下是如何更改使其与.NET Core和.NET 5+兼容。
唯一需要更改的功能是AdminRelauncher,它应该像这样改变。
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());
}
}
}
唯一的重大变化就是,如有人指出应用程序并不总是可用。所以Environment.Exit(0)可以替换它,并且文件名需要将 .exe 替换为 .dll。这在 .NET 6 中已经过测试。
Application.Current.Shutdown();
并使用return
退出Main()
即可正常工作。 - Ge Rong对于那些未选中“启用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中找到此选项,请右键单击您的项目==>属性==>选择安全性==>启用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"})
编辑:但是如果您以这种方式部署,则一些杀毒软件将阻止您的代码。
打开你的 app.Manifest 文件,你会看到这个:
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
注释里面有说明,但只需删除“requireAdministrator”并插入以下内容即可解决我的问题:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
asInvoke
。 - SkiSharp
requireAdministrator
权限,并且 ClickOnce 抱怨它不支持requireAdministrator
。问题非常明显。 ClickOnce 在您的清单中看到了需要提升权限(这成为您的应用程序的一部分)。我不确定您还需要什么信息... - Ken White