Get local system users

by on Feb.21, 2013, under C#

Use the following code to retrieve local system users and some of their properties, using WMI and Management namespace .
Method

public List<User> GetUsers()
{
    try
    {
	List<User> users = new List<User>();
	foreach (ManagementObject mo in
		(new ManagementObjectSearcher(new SelectQuery("Win32_UserAccount"))).Get())
	{
		users.Add(new User
		{
			Name = mo["Name"].ToString(),
			Caption = mo["Caption"].ToString(),
			IsDisabled = Boolean.Parse(mo["Disabled"].ToString()),
			IsLocalAccount = Boolean.Parse(mo["LocalAccount"].ToString()),
			IsPasswordChangeable = Boolean.Parse(mo["PasswordChangeable"].ToString()),
			PasswordExpires = Boolean.Parse(mo["PasswordExpires"].ToString()),
			IsPasswordRequired = Boolean.Parse(mo["PasswordRequired"].ToString())
		});
	}
	return users;
    }
    catch
    {
	return null;
    }
}

User class

class User
{
    private String _Name, _Caption;
    private Boolean _Disabled, _LocalAccount,
 	_PasswordChangeable, _PasswordExpires, _PasswordRequired;
 
    public String Name
    {
	get
	{
		return _Name;
	}
	set
	{
		_Name = value;
	}
    }
 
    public String Caption
    {
	get
	{
		return _Caption;
	}
	set
	{
		_Caption = value;
	}
    }
 
    public Boolean IsDisabled
    {
	get
	{
		return _Disabled;
	}
	set
	{
		_Disabled = value;
	}
    }
 
    public Boolean IsLocalAccount
    {
	get
	{
		return _LocalAccount;
	}
	set
	{
		_LocalAccount = value;
	}
    }
 
    public Boolean IsPasswordChangeable
    {
	get
	{
		return _PasswordChangeable;
	}
	set
	{
		_PasswordChangeable = value;
	}
    }
 
    public Boolean PasswordExpires
    {
        get
	{
		return _PasswordExpires;
	}
	set
	{
		_PasswordExpires = value;
	}
    }
 
    public Boolean IsPasswordRequired
    {
	get
	{
		return _PasswordRequired;
	}
 	set
	{
		_PasswordRequired = value;
	}
    }
}
:, , , , , ,

%d bloggers like this: