LMS*_*LMS 88 c# wpf clickonce compiler-errors
所以我正在编写一个需要访问注册表的应用程序.我没有触及任何构建设置,希望在添加其他触摸(例如描述或名称)之前让事情正常工作.
出乎意料的是,我得到了一个不会消失的错误.ClickOnce does not support the request execution level 'requireAdministrator'.现在,我没有在这个应用程序中触及ClickOnce.我所做的就是包括一个请求这些权限的清单文件.
我现在的问题是这个错误不会消失,我无法编译我的程序.关于该怎么做的任何建议?(旁注:我准备去睡觉了,明天下午我会检查一下).
LMS*_*LMS 141
编辑:这个评论也给出了一个很好的答案.
无论您是否想要,只要点击"发布",点击一次即可启用!如果您使用"requireAdministrator",则表明您无法使用ClickOnce,因此无法"发布"您的项目.
原版的:
事实证明,在"安全"选项卡下,选中了"启用ClickOnce安全设置".即使我没有检查它.无论如何,取消选中停止ClickOnce给我的错误.这需要一段时间才能找到......
t_p*_*lus 41
我知道这是一个老问题,但两年后我来到这里是这样的:
您可以从项目属性的"安全"选项卡中禁用ClicKOnce以帮助解决问题; 见下文:

Tyl*_*r C 12
我知道这已经过时了,但我偶然发现它寻找答案.在我的情况下,我使用发布功能,我需要继续使用它.我还需要访问管理功能.因此,上述答案都不适合我.
我最后在我的应用程序的最开始添加了一个方法,检查它是否以管理员身份运行,如果不是,则重新启动自己作为管理员.为此,您需要添加以下参考.
using System;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;
Run Code Online (Sandbox Code Playgroud)
然后你需要把它放在你的主方法有方便访问的地方.我正在使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,在程序开始时,添加对方法的引用.在我的情况下,我将它添加到MainWindow,但将其添加到Main也可以.
public MainWindow()
{
InitializeComponent();
AdminRelauncher(); //This is the only important line here, add it to a place it gets run early on.
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
对于那些使用取消选中“启用ClickOnce安全设置”无法工作的人,可以尝试我找到的方法。
首先,将您的 app.manifest requestsExecutionLevel 项目保留原样:
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
Run Code Online (Sandbox Code Playgroud)
然后你像这样编辑你的 Program.cs 文件:
using System;
using System.Diagnostics;
using System.Reflection;
using System.Security.Principal;
using System.Windows.Forms;
Run Code Online (Sandbox Code Playgroud)
重构 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());
}
}
Run Code Online (Sandbox Code Playgroud)
它适用于 Windows 10 和 Visual Studio 2019!
可以通过选择“启用 ClickOnce 安全设置”(因为在发布期间不能“取消选中”,如上所述)然后选择“这是一个部分信任的应用程序”来实现此操作。下拉菜单中会自动选择“Local Intranet”,非常好。
| 归档时间: |
|
| 查看次数: |
58707 次 |
| 最近记录: |