zoukankan      html  css  js  c++  java
  • c# 判断当前用户是否是管理员权限启动应用程序。

    .

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp4
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
    
                /**
                 * 当前用户是管理员的时候,直接启动应用程序
                 * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
                 */
                //获得当前登录的Windows用户标示
                System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
                System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
                //判断当前登录用户是否为管理员
                if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
                {
                    //如果是管理员,则直接运行
                    Application.Run(new Form1());
                }
                else
                {
                    //创建启动对象
                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                    startInfo.UseShellExecute = true;
                    startInfo.WorkingDirectory = Environment.CurrentDirectory;
                    startInfo.FileName = Application.ExecutablePath;
                    //设置启动动作,确保以管理员身份运行
                    startInfo.Verb = "runas";
                    try
                    {
                        System.Diagnostics.Process.Start(startInfo);
                    }
                    catch
                    {
                        return;
                    }
                    //退出
                    Application.Exit();
                }
    
    
    
    
    
            }
    
    
    
    
    
    
    
    
        }
    }
    

      

  • 相关阅读:
    spring四种依赖注入方式
    java利用反射来调用一个类的私有方法
    IOC和AOP的基本概念
    开业大吉
    1752年9月奇怪的日历
    找到一个软件测试的学习网址,保留一下
    学习任务
    操作系统的第一次作业
    答题
    第四章读后感
  • 原文地址:https://www.cnblogs.com/wgscd/p/13793106.html
Copyright © 2011-2022 走看看