zoukankan      html  css  js  c++  java
  • C#判断程序是否以管理员运行

        /// <summary>
             /// 判断程序是否是以管理员身份运行。
             /// </summary>
             public static bool IsRunAsAdmin()
             {
                 WindowsIdentity id = WindowsIdentity.GetCurrent();
                 WindowsPrincipal principal = new WindowsPrincipal(id);
                 return principal.IsInRole(WindowsBuiltInRole.Administrator);
             }

       //不是以管理员身份开启,则自动以管理员身份重新打开程序
       //写在构造里比较省资源
       public LoginFrm()
             {
                 try
                 {
                     //判断是否以管理员身份运行,不是则提示
                     if (!PublicUtil.IsRunAsAdmin())
                     {
                         ProcessStartInfo psi = new ProcessStartInfo();
                         psi.WorkingDirectory = Environment.CurrentDirectory;
                         psi.FileName = Application.ExecutablePath;
                         psi.UseShellExecute = true;
                         psi.Verb = "runas";
                         Process p = new Process();
                         p.StartInfo = psi;
                         p.Start();
                         Process.GetCurrentProcess().Kill();
                     }
                 }
                 catch (Exception ex)
                 {
                     ExceptionScheduler.ExceptionScheduler exceptionScheduler = new ExceptionScheduler.ExceptionScheduler(ex);
                     ShowMessageOnUI.ShowErrorMessage("程序无法获取Windows管理员身份运行, 请手动使用Windows管理员身份运行");
                 }
                 InitializeComponent();
             }

  • 相关阅读:
    CodeSmith注册错误的解决方法
    我是“坚守者”还是"背叛者"?
    拿什么留住你,我的程序员
    去除HTML代码得函数
    页面之间传递参数得几种方法
    nhibernate source code analyzed (abstract classes in nhibernate2.0)
    Web 2.0时代RSS的.Net实现
    Visual Studio.net 2003安装提示重启问题
    开放思路,综合考虑,心胸开阔,做一个合格的项目经理
    了解实际开发中 Hashtable 的特性原理 .NET, JAVA, PHP
  • 原文地址:https://www.cnblogs.com/hrkblogs/p/9133154.html
Copyright © 2011-2022 走看看