I have the following problem: I want to get the the logged in user with a WMI class. So I tried this:
try
{
ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROMWin32_LoggedOnUser");
foreach (ManagementObject queryObj in searcher.Get())
{
accounttype += queryObj["Antecedent"];
}
}
catch
{
accounttype = "error";
}
But this don't work because the queryObj returns a reference to Win32_Account
!
I have no Idea how I can read the values of this Win32_Account
reference!
BTW,
I know there are other ways ( like Environment.UserName
, but I want to generally understand these reverences!
Thanks!
The
Antecedent
andDependent
properties of theWin32_LoggedOnUser
WMI class returns aWMI Object Path
, which is a unique id for a WMI class instance, you can access the data of to this class creating a instance to theManagementObject
object and then setting the propertyPath
obtained from aManagementPath
object.Try this sample