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

    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开机不能自动运行的问题等

    我写个例子供大家参考:

            static void Main()
            {
                if (!CheckAdministratorGrant())
                {
                    return;
                }
    
                    // We are running as administrator
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form1());
            }
    
            private static bool CheckAdministratorGrant()
            {
                var wi = WindowsIdentity.GetCurrent();
                var wp = new WindowsPrincipal(wi);
    
                bool runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator);
    
                if (!runAsAdmin)
                {
                    // 直接管理者としてClickOnceアプリを起動することはできません。
                    // その代わりに我々は新しいプロセスの管理者としてアプリケーションを起動します。
                    var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase)
                    {
                        // 次のプロパティは、新しいプロセスを管理者として実行します
                        UseShellExecute = true,
                        Verb = "runas"
                    };
    
                    // 新しいプロセスを開始する
                    try
                    {
                        Process.Start(processInfo);
                    }
                    catch (Exception)
                    {
                        // ユーザーは、アプリケーションを管理者として実行することを許可しませんでした
                        MessageBox.Show("すみませんが、私は管理者権利でこのプログラムを始めることができないようです! ");
                    }
    
                    // 現在のプロセスをシャットダウンする
                    Application.Exit();
                }
                else
                {
                    return true;
                }
                return false;
            }
    

      

  • 相关阅读:
    使用Dubbox构架分布式服务
    Elasticsearch搜索之explain评分分析
    Elasticsearch搜索之most_fields分析
    Elasticsearch搜索之cross_fields分析
    Elasticsearch搜索之best_fields分析
    Elasticsearch高级搜索排序( 中文+拼音+首字母+简繁转换+特殊符号过滤)
    ibatis2.3中#和$符号的区别(转)
    读文章有感
    图片bmp格式转换为jpg格式
    IndexOf、LastIndexOf、Substring的用法
  • 原文地址:https://www.cnblogs.com/wzihan/p/14843666.html
Copyright © 2011-2022 走看看