这是我检索“我的本地计算机信息”的代码:
ManagementObjectSearcher Usersearcher = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
ManagementObjectCollection Usercollection = Usersearcher.Get();
string[] sep = { "\\" };
string[] UserName = Usercollection.Cast<ManagementBaseObject>().First()["UserName"].ToString().Split(sep, StringSplitOptions.None);
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT * FROM Win32_UserAccount where Domain = 'MyDomain' and Name= '" + UserName[1] + "'");
ManagementObjectCollection collection = searcher.Get();
WriteOnListBox("UserName: " + (string)collection.Cast<ManagementBaseObject>().First()["Name"]);
WriteOnListBox("Full Name: " + (string)collection.Cast<ManagementBaseObject>().First()["FullName"]);
WriteOnListBox("Description: " + (string)collection.Cast<ManagementBaseObject>().First()["Description"]);
但我无法找回我所在的小组和电子邮件。
答案 0 :(得分:4)
经过几个小时的努力,我解决了它,我将回答我的问题,
private string GetGroupsForUser(string UserName)
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_GroupUser where PartComponent=\"Win32_UserAccount.Domain='MyDomain',Name='" + UserName + "'\"");
StringBuilder strGroups = new StringBuilder();
foreach (ManagementObject mObject in searcher.Get())
{
ManagementPath path = new ManagementPath(mObject["GroupComponent"].ToString());
if (path.ClassName == "Win32_Group")
{
String[] names = path.RelativePath.Split(',');
strGroups.Append(names[1].Substring(names[1].IndexOf("=") + 1).Replace('"', ' ').Trim() + ", ");
}
}
return strGroups.ToString();
}