Solved

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

Posted on 2004-09-07
6
458 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now