首页 > 代码库 > WPF 检测管理员权限

WPF 检测管理员权限

        // 检查是否是管理员身份           private void CheckAdministrator()        {            try            {                var wi = WindowsIdentity.GetCurrent();                var wp = new WindowsPrincipal(wi);                bool runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator);                if (!runAsAdmin)                {                    var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase)                    {                        UseShellExecute = true,                        Verb = "runas"                    };                    Process.Start(processInfo);                    Current.Shutdown();                }            }            catch            {                Current.Shutdown();            }        }        //配置启动项        protected override void OnStartup(StartupEventArgs e)        {            base.OnStartup(e);            CheckAdministrator();        }

 

WPF 检测管理员权限