如何在windows c#窗体中禁用ClickOnce安全性



嘿,我刚刚制作了我的c#程序,它需要管理员权限才能访问某些文件,但在发布应用程序时,它显示了一个构建错误("clickonce不支持需要管理员"(,我曾尝试禁用clickoncce复选框,但它仍然被启用。请帮忙。

感谢这篇文章让我找到了问题的解决方案。希望它能帮助你。https://www.codeproject.com/tips/627850/clickonce-deployment-vs-requestedexecutionlevel-eq

你留下你的清单如下:

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

在Program.cs 中添加代码

static class Program
{

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

最新更新