blog

Home / DeveloperSection / Blogs / Dispaying user account info in c#.

Dispaying user account info in c#.

Anonymous User5490 10-May-2011

In this blog I will tell you that how to retrieve basic user account information on computer by using WMI classes. In this blog we see that how to retrieve number of user accounts and their details in c#. For implementing this task I have used System.Management class which resides in System.Management.dll.

using System;
using System.Management;     //This namespace required to query with WMI namespacces.
namespace UserInfo1
{
    class Program
    {
        static void Main(string[] args)
        {
            SelectQuery query = new SelectQuery("Win32_UserAccount");    //Create an object of SelectQuery class and pass Win32_UserAccount as parameter.
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);  //Create an object of management object searcher class and pass SelectQuery object as parameter.
            //Iterate information of management object server by using foreach.
            foreach (ManagementObject obj in searcher.Get())
            {
                Console.WriteLine("User Name  :  {0}", obj["Name"]);   //Retrive name of user.
                Console.WriteLine("User Caption  :  {0}", obj["Caption"]);   //Retrive caption which is displayed on logon screen.
                Console.WriteLine("User Description  :  {0}", obj["Description"]);    //Describe user description.
                Console.WriteLine("Domain  :  {0}", obj["Domain"]);    //Display domain of accounts.
                Console.WriteLine("Password Changable  :  {0}", obj["PasswordChangeable"]);    //Display whether password is chanjable or not.
                Console.WriteLine("User SID  :  {0}", obj["SID"]);   //Display user SID.
                Console.WriteLine("User Status   :  {0}", obj["Status"]);   //Display status of user.
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}

Output of the following code snippet is as follows

User Name  :  Administrator

User Caption  :  AAA-PC3\Administrator

User Description  :  Built-in account for administering the computer/domain

Domain  :  AAA-PC3

Password Changable  :  True

User SID  :  S-1-5-21-142572773-4237543349-1489353810-500

User Status   :  Degraded

User Name  :  BBBB

User Caption  :  AAA-PC3\BBBB

User Description  :

Domain  :  AAA-PC3

Password Changable  :  True

User SID  :  S-1-5-21-142572773-4237543349-1489353810-1008

User Status   :  OK

User Name  :  Guest

User Caption  :  AAA-PC3\Guest

User Description  :  Built-in account for guest access to the computer/domain

Domain  :  AAA-PC3

Password Changable  :  False

User SID  :  S-1-5-21-142572773-4237543349-1489353810-501

User Status   :  Degraded

User Name  :  AAA

User Caption  :  AAA-PC3\AAA

User Description  :

Domain  :  AAA-PC3

Password Changable  :  True

User SID  :  S-1-5-21-142572773-4237543349-1489353810-1000

User Status   :  OK


Updated 18-Sep-2014
I am a content writter !

Leave Comment

Comments

Liked By