Solved

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

Posted on 2004-09-07
6
455 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:mnasman
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 22

Accepted Solution

by:
mnasman 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:mnasman
ID: 12013421
Glad to help you :-)
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

705 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

15 Experts available now in Live!

Get 1:1 Help Now