• 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();
             }

  • 相关阅读:
    并发包学习(二)-容器学习记录
    初尝微信小程序开发与实践
    记一次全站升级https引发的一系列问题
    Hadoop集群搭建
    es5 的类和继承
    TypeScript 类和继承
    TypeScript 函数
    TypeScript 变量 和 数据类型
    js变量提升与函数提升
    vue 路由监听
  • 原文地址:https://www.cnblogs.com/hrkblogs/p/9133154.html
Copyright © 2020-2023  润新知