zoukankan      html  css  js  c++  java
  • [转载]解决clickonce不支持administer权限问题

         转自ClickOnce deployment vs. requestedExecutionLevel = requireAdministrator

         ClickOnce方式部署应用简单方便,估计很多人都用过,但这种方式存在一定的“缺陷”,即以管理员方式启动应用的问题,虽然出于安全考虑可以理解,但给需要管理员权限才能正常运行的程序带来了一定的麻烦,这导致部分人员放弃了ClickOnce发布。

         经过查找相关资料,发现还是有办法解决这个问题的,具体操作如下:

         1、保留manifest文件不变。

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

         2、然后编辑Program.cs文件如下:

    using System;
    using System.Diagnostics;
    using System.Reflection;
    using System.Security.Principal;
    using System.Windows.Forms;
    
    namespace MyProgramNamespace
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [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());
                }
            }
        }
    }  

        也可参考《以管理员身份启动ClickOnce部署的应用程序》,不过该文章方法我没有调试成功,主要是我C#基础不行。

        开机自启动解决权限问题,也可参考《VS编写程序主动要求系统管理员权限(UAC控制)》和《Win7如何提升为管理员权限,如何开机启动需要管理员权限的程序,解决Win7开机不能自动运行的问题等

  • 相关阅读:
    js-排序算法
    django csrf token添加
    django mongodb配置
    django logging
    linux文件行首行尾添加或替换
    linux 大小写转化
    linux $参数
    mysql 基本操作
    生产者消费者示例
    python smtplib发email
  • 原文地址:https://www.cnblogs.com/MobileBo/p/6666946.html
Copyright © 2011-2022 走看看