[英]Find if specified user (string) is a member of local Administrators group
I am trying to find if a user I identify is a member of the local Administrators group. 我正在尝试确定我确定的用户是否是本地Administrators组的成员。
But my code does nothing... 但是我的代码什么都不做...
Please see me code below. 请在下面查看我的代码。
Also, this is being executed in my public void Form1_Load(object sender, EventArgs e) {}
so it is done every time at application start up. 另外,这是在我的public void Form1_Load(object sender, EventArgs e) {}
因此每次在应用程序启动时都会执行此操作。
string localUser = WindowsIdentity.GetCurrent().Name.ToString();
char[] trimmingsEnd = { 'a', 'd', 'm', 'i', 'n' };
string trimmedlocalEnd = localUser.TrimEnd(trimmingsEnd);
char[] trimmingsFront = { 'C', 'o', 'm', 'p', 'u', 't', 'e', 'r', '\\' };
string trimmedlocalUser = trimmedlocalEnd.TrimStart(trimmingsFront);
WindowsIdentity windowsIdentity = new WindowsIdentity(trimmedlocalUser);
WindowsPrincipal principal = new WindowsPrincipal(windowsIdentity);
bool IsAdmin = principal.IsInRole("BUILTIN\\" + "Administrators");
if (IsAdmin == false)
MessageBox.Show("not part of admin");
if (IsAdmin == true)
MessageBox.Show("part of admin");
If the program is compiling without any issues then it may be that the event handler is not setup for Form1_Load(). 如果程序正在编译而没有任何问题,则可能是未为Form1_Load()设置事件处理程序。
You may have to add to the Form1.Designer.cs file something like: 您可能需要向Form1.Designer.cs文件中添加以下内容:
this.Load += new System.EventHandler(Form1_Load);
Let me know if this helps. 让我知道是否有帮助。
So, I ditched the method above, as all I could find is the current user...but I needed to search for two user names in the local administrators group. 因此,我放弃了上面的方法,因为我只能找到当前用户...但是我需要在本地管理员组中搜索两个用户名。
The following code worked perfectly for what I needed! 以下代码可完美满足我的需求! Hope this helps someone. 希望这对某人有帮助。
//Get all users from the local Administrators group and create list
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry admGroup = localMachine.Children.Find("administrators", "group");
object members = admGroup.Invoke("members", null);
List<string> userList = new List<string>();
//Get current user
string localUser1 = WindowsIdentity.GetCurrent().Name.ToString();
//Take domain name off
char[] trimmingsFront = { 'D', 'O', 'M', 'A', 'I', 'N', '\\' };
string trimmedlocalFront = localUser1.TrimStart(trimmingsFront);
//Take "admin" off username
char[] trimmingsEnd = { 'a', 'd', 'm', 'i', 'n' };
string trimmedlocalUser = trimmedlocalFront.TrimEnd(trimmingsEnd);
//Add each local Administrator to list
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
userList.Add(member.Name);
}
//Check if users are not part of list
if (!(userList.Contains(trimmedlocalFront)))
MessageBox.Show(trimmedlocalFront + " is not a member of the local Administrators group. The drag and drop functionality will not work unless " + trimmedlocalFront + " is a member of the local Administrators group. After " + trimmedlocalFront + " is added, please restart your machine for the changes to take effect.", "Local Administrator Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
else if (!(userList.Contains(trimmedlocalUser)))
MessageBox.Show(trimmedlocalUser + " is not a member of the local Administrators group. The drag and drop functionality will not work unless " + trimmedlocalUser + " is a member of the local Administrators group. After " + trimmedlocalUser + " is added, please restart your machine for the changes to take effect.", "Local Administrator Warning", MessageBoxButtons.OK, MessageBoxIcon.Stop);
If you want to check for your own user names, for the 'if', do: 如果要检查自己的用户名,请为“ if”检查:
if (!(userList.Contains(whateverusernameyouwanttosearch)))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.