zoukankan      html  css  js  c++  java
  • C# 默认以管理员权限运行程序

     /** 
                 * 当前用户是管理员的时候,直接启动应用程序 
                 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行 
                 */
                //获得当前登录的Windows用户标示
            //URL:http://www.bianceng.cn/Programming/csharp/201410/45784.htm
                System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();  
                //创建Windows用户主题  
                Application.EnableVisualStyles();  
          
                System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);  
                //判断当前登录用户是否为管理员  
                if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))  
                {  
                    //如果是管理员,则直接运行  
          
                    Application.EnableVisualStyles();  
                    Application.Run(new Form1());  
                }  
                else
                {  
                    //创建启动对象  
                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();  
                    //设置运行文件  
                    startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;  
                    //设置启动参数  
                    startInfo.Arguments = String.Join(" ", Args);  
                    //设置启动动作,确保以管理员身份运行  
                    startInfo.Verb = "runas";  
                    //如果不是管理员,则启动UAC  
                    System.Diagnostics.Process.Start(startInfo);  
                    //退出  
                    System.Windows.Forms.Application.Exit();  
                } 
  • 相关阅读:
    线段树快速查找区间值
    html学习笔记
    区块链是怎么运行的
    【C++ 流类库与输入输出 】实验七
    【C++ 实验六 继承与派生】
    10天冲刺第四天后端app开发
    10天冲刺第三天后端app开发
    10天冲刺第二天之完成后端
    第二次冲刺第一天之后台管理
    第一阶段--冲刺总结
  • 原文地址:https://www.cnblogs.com/ficow/p/5436666.html
Copyright © 2011-2022 走看看