zoukankan      html  css  js  c++  java
  • C# Winform程序请求管理员权限

    如果你的Winform程序需要管理员权限才能正常执行,请加入如下代码:

    static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main(String[] Args)
            {
                //获得当前登录的Windows用户标示 
                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.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new mainForm());
                }
                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();
                }
            }
        }
  • 相关阅读:
    IDEA控制台输出中文乱码问题
    JAVA web 框架集合
    去掉VSS控制
    .Net Core .Net Core V1.0 创建MVC项目
    .Net Core .Net Core的学习
    WebService 天气预报webservice接口
    SMS106 短信验证码接口测试
    Regex 常用的正则表达式
    Jquery Plugins Jquery Validate
    MVC 路由调试工具-RouteDebugger
  • 原文地址:https://www.cnblogs.com/halfmanhuang/p/3987523.html
Copyright © 2011-2022 走看看