WPF 检测管理员权限

        // 检查是否是管理员身份   

        private static void CheckAdministrator()

        {

            WindowsIdentity wi = null;

            try

            {

                wi = WindowsIdentity.GetCurrent();

                if (wi == null) throw new Exception("未将对象仅用到对象的实例!");

                var wp = new WindowsPrincipal(wi);

                var runAsAdmin = wp.IsInRole(WindowsBuiltInRole.Administrator);

                if (runAsAdmin) return;

                var processInfo = new ProcessStartInfo(Assembly.GetExecutingAssembly().CodeBase)

                {

                    UseShellExecute = true,

                    Verb = "runas"

                };

                

                Process.Start(processInfo);

            }

            catch

            {

                Console.WriteLine("失败!");

            }

            finally

            {

                if (wi != null)

                    wi.Dispose();

            }

        }

 

你可能感兴趣的:(WPF)