Solved

How to get windows domain list in a combo box (asp.net)

Posted on 2004-09-07
6
460 Views
Last Modified: 2008-03-17
Hi All,

Does any one know how to get the list of all available windows domains (domain list) into a combo box? Similar to the combo box that we get while logging into windows.

Thanks
Konda
0
Comment
Question by:madhukar_konda
  • 3
  • 3
6 Comments
 

Author Comment

by:madhukar_konda
ID: 11995257
Previously I got the following answer from TheLearnedOne.

Public Function EnumerateDomains() As ArrayList

    Dim arrayDomains As New ArrayList

    Try

      Dim queryString As String = "Select domain From Win32_ComputerSystem"

      Dim searchDomains As New Management.ManagementObjectSearcher(queryString)

      For Each domainCurrent As Management.ManagementBaseObject In searchDomains.Get
        arrayDomains.Add(domainCurrent.Properties("Domain").Value)
      Next domainCurrent

    Catch ex As Exception
      MessageBox.Show(ex.ToString)
    End Try

    Return arrayDomains

  End Function 'EnumerateDomains'

But, this function is returning currently user logged in domain. I want all the existing domains. Any ideas??
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 11995571
Hello

   Try this code

private void button1_Click(object sender, System.EventArgs e)
            {
                  StringCollection adDomains = this.GetDomainList();
                  foreach (string strDomain in adDomains)
                  {
                        comboBox1.Items.Add(strDomain);
                  }
            }



            private StringCollection GetDomainList()
            {
                  StringCollection domainList = new StringCollection();
                  try
                  {
                        DirectoryEntry en = new DirectoryEntry("LDAP://");
                        // Search for objectCategory type "Domain"
                        DirectorySearcher srch = new DirectorySearcher("objectCategory=Domain");
                        SearchResultCollection coll = srch.FindAll();
                        // Enumerate over each returned domain.
                        foreach (SearchResult rs in coll)
                        {
                              ResultPropertyCollection resultPropColl = rs.Properties;
                              foreach( object domainName in resultPropColl["name"])
                              {
                                    domainList.Add(domainName.ToString());
                              }
                        }
                  }
                  catch (Exception ex)
                  {
                        MessageBox.Show (ex.Message);
                  }
                  return domainList;
            }

HTH

Regards,
Mohammed
0
 

Author Comment

by:madhukar_konda
ID: 11995719
Hi Mohammed.

Thanks for your reply. But it is giving me only one domain to which i logged in. any ideas?

I want all the available domains in the network. Similar to windows login screen domain list.
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 500 total points
ID: 12004681
Hello  madhukar_konda

  Here's a simplier way, and it should return all the domain in your network

public ArrayList GetDomains()
{
      DirectoryEntry dn = new DirectoryEntry("WinNT:");
      ArrayList Domains = new ArrayList();
      try                  
      {
            foreach (DirectoryEntry chd in dn.Children)
            {
                  Domains.Add(chd.Name);                              
            }
      }
      catch (Exception ex)
      {
            MessageBox.Show(ex.Message);
      }                  
      return Domains;
}

private void button2_Click(object sender, System.EventArgs e)
{
      foreach (string s in GetDomains())
      comboBox1.Items.Add(s);
}

Regards,
Mohammed
0
 

Author Comment

by:madhukar_konda
ID: 12013410
Thanks Mohammed!

It does work. I really appriciate your quick response.
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 12013421
Glad to help you :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question