zoukankan      html  css  js  c++  java
  • WinForm 代码实现以管理员身份运行

    [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.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(); 
    } 
    }
  • 相关阅读:
    看别人的代码学习的css
    Font Awesome
    响应式网站设计
    css兼容性的问题
    英语
    我的bootstrap使用的历程
    jquery的常用的容易忘记的东西
    jquery基本方法
    js与jquery的区别
    134123
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/7250065.html
Copyright © 2011-2022 走看看