You can work out if a local account exists through the System.Security.Principal namespace using the following code.
bool AccountExists(string name)
{
bool bRet = false;
try
{
NTAccount acct = new NTAccount(name);
SecurityIdentifier id = (SecurityIdentifier)acct.Translate(typeof(SecurityIdentifier));
bRet = id.IsAccountSid();
}
catch (IdentityNotMappedException)
{
/* Invalid user account */
}
return bRet;
}
Now getting group membership is slightly harder, you can easily do it for the current user using the WindowsPrinciple.IsInRole method (creating a principle from the WindowsIdentify.GetCurrent() method).
As pointed out I don't think there is a way of getting anything else without resorting to pinvoke or WMI. So here is a bit of code to check group membership with WMI.
bool IsUserInGroup(string name, string group)
{
bool bRet = false;
ObjectQuery query = new ObjectQuery(String.Format("SELECT * FROM Win32_UserAccount WHERE Name='{0}' AND LocalAccount=True", name));
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection objs = searcher.Get();
foreach (ManagementObject o in objs)
{
ManagementObjectCollection coll = o.GetRelated("Win32_Group");
foreach (ManagementObject g in coll)
{
bool local = (bool)g["LocalAccount"];
string groupName = (string)g["Name"];
if (local && groupName.Equals(group, StringComparison.InvariantCultureIgnoreCase))
{
bRet = true;
break;
}
}
}
return bRet;
}