How do I know if an user account exists on my Windows OS (Vista)? I need this information from a stand alone machine that hasn't joined any domain.
I want to know whether an user is a part of a group, for example is a user 'admin' part of 'Administrators' group or not?
I have tried the following code and is working fine for me..
public bool IsUserMemberOfGroup(string userName, string groupName)
{
bool ret = false;
try
{
DirectoryEntry localMachine = new DirectoryEntry("WinNT://" + Environment.MachineName);
DirectoryEntry userGroup = localMachine.Children.Find(groupName, "group");
object members = userGroup.Invoke("members", null);
foreach (object groupMember in (IEnumerable)members)
{
DirectoryEntry member = new DirectoryEntry(groupMember);
if (member.Name.Equals(userName, StringComparison.CurrentCultureIgnoreCase))
{
ret = true;
break;
}
}
}
catch (Exception ex)
{
ret = false;
}
return ret;
}