本文介绍了在 .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# 中测试进程是否具有管理权限
发布评论