How to list all Windows Users

I had to figure out how to write all currently constructed Windows Users to the screen and managed it with this little piece of C# code:

using System.Management;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SelectQuery query = new SelectQuery("Win32_UserAccount");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
            foreach (ManagementObject envVar in searcher.Get())
            {
                Console.WriteLine("Username : {0}", envVar["Name"]);
            }

            Console.ReadLine();

        }
    }
}

Depending on the PC it might eat up a little more time sometimes.

2 comments ↓

#1 raju on 10.10.07 at 10:25 am

Thanks for the code!

#2 merawa on 03.22.08 at 9:42 pm

thank you very much,
but i have a problem, why when i change the name of ana account the running result is still the same, the result not update? if the account name = student and i change it to teacher the result stay as the first run (student) how can i solve this problem, please tell me

Leave a Comment