.NET: How to query NT NT / Users database or entry?

how can we query the NT / Users database for all users on the machine?

0


a source to share


2 answers


I am assuming you are using C #. You can get them using WMI:



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();
        }
    }
}

      

+2


a source


You can use the System.DirectoryServices namespace for this. Here's a great article describing how to use the classes in this namespace.

Here's some code showing how to do it:



DirectoryEntry entry = new DirectoryEntry("WinNT://MACHINE_NAME");
entry.AuthenticationType = AuthenticationTypes.Secure;

DirectorySearcher deSearch = new DirectorySearcher(entry);
deSearch.Filter = "(&(objectClass=user))";

SearchResultCollection results = deSearch.FindAll();

foreach (SearchResult srUser in results)
{
      try
      {
            DirectoryEntry de = srUser.GetDirectoryEntry();
            lstbox.Items.Add(de.Properties["sAMAccountName"].Value.ToString());
      }
      catch { }
}

      

+3


a source







All Articles