Programmatically get the number of Windows user accounts

I need a way to get a list of all user accounts on a computer (Windows), I need to get this information through C # .NET 1.1 + 2003

+3
source share
2 answers

You can use WMI to list local users and groups:

class Program
{
    static void Main(string[] args)
    {
        SelectQuery sQuery = new SelectQuery("Win32_UserAccount", "Domain='mypcname'");

        try
        {
            ManagementObjectSearcher mSearcher = new ManagementObjectSearcher(sQuery);

            Console.WriteLine("User Accounts");
            Console.WriteLine();

            foreach (ManagementObject mObject in mSearcher.Get())
            {
                Console.WriteLine("Account {0}", mObject["Name"]);
                foreach (PropertyData prop in mObject.Properties)
                {
                    Console.WriteLine("Name: {0}\tValue: {1}", prop.Name, prop.Value);
                }
                Console.WriteLine();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

        Console.ReadKey();
    }
}

The above code requires a link to System.Management, and you need to replace "mypcname" with the name of your machine.

+7
source

You will need to use WMI to get the information you need.

Add a link to System.Management, and the code below will do exactly what you need :)

using System;
using System.Management;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            ManagementObjectSearcher mos = new ManagementObjectSearcher("Select * from Win32_UserAccount Where LocalAccount = True");
            ManagementObjectCollection results = mos.Get();

            foreach (ManagementObject user in results)
            {
                Console.WriteLine("Account Type: " + user["AccountType"].ToString());
                Console.WriteLine("Caption: " + user["Caption"].ToString());
                Console.WriteLine("Description: " + user["Description"].ToString());
                Console.WriteLine("Disabled: " + user["Disabled"].ToString());
                Console.WriteLine("Domain: " + user["Domain"].ToString());
                Console.WriteLine("Full Name: " + user["FullName"].ToString());
                Console.WriteLine("Local Account: " + user["LocalAccount"].ToString());
                Console.WriteLine("Lockout: " + user["Lockout"].ToString());
                Console.WriteLine("Name: " + user["Name"].ToString());
                Console.WriteLine("Password Changeable: " + user["PasswordChangeable"].ToString());
                Console.WriteLine("Password Expires: " + user["PasswordExpires"].ToString());
                Console.WriteLine("Password Required: " + user["PasswordRequired"].ToString());
                Console.WriteLine("SID: " + user["SID"].ToString());
                Console.WriteLine("SID Type: " + user["SIDType"].ToString());
                Console.WriteLine("Status: " + user["Status"].ToString());
            }
            Console.ReadKey();
        }
    }
}
+4
source

Source: https://habr.com/ru/post/1714486/


All Articles