在 .NET/C# 中测试进程是否具有管理权限

编程入门 行业动态 更新时间:2024-10-26 08:26:00
本文介绍了在 .NET/C# 中测试进程是否具有管理权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有一种规范的方法来测试进程是否在机器上具有管理权限?

Is there a canonical way to test to see if the process has administrative privileges on a machine?

我将开始一个长时间运行的进程,在进程生命周期的后期,它将尝试一些需要管理员权限的事情.

I'm going to be starting a long running process, and much later in the process' lifetime it's going to attempt some things that require admin privileges.

我希望能够预先测试流程是否拥有这些权限,而不是稍后进行测试.

I'd like to be able to test up front if the process has those rights rather than later on.

推荐答案

这将检查用户是否在本地管理员组中(假设您没有检查域管理员权限)

This will check if user is in the local Administrators group (assuming you're not checking for domain admin permissions)

using System.Security.Principal; public bool IsUserAdministrator() { //bool value to hold our return value bool isAdmin; WindowsIdentity user = null; try { //get the currently logged in user user = WindowsIdentity.GetCurrent(); WindowsPrincipal principal = new WindowsPrincipal(user); isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator); } catch (UnauthorizedAccessException ex) { isAdmin = false; } catch (Exception ex) { isAdmin = false; } finally { if (user != null) user.Dispose(); } return isAdmin; }

更多推荐

在 .NET/C# 中测试进程是否具有管理权限

本文发布于:2023-11-11 06:48:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577672.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理权限   进程   测试   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!