Friday, November 19, 2010

How can i programtically find all the domains in my local network in dot net?

I need to list all the network domains(mshome, workgroup...) in my LAN . How can i get the list of all those domains programaticaslly in dot net?How can i programtically find all the domains in my local network in dot net?
private void Page_Load(object sender, System.EventArgs e)

{

StringCollection adDomains = this.GetDomainList();

foreach (string strDomain in adDomains)

{

Response.Write(';%26lt;br%26gt;'; + strDomain + ';%26lt;/b%26gt;';);

}

}



private StringCollection GetDomainList()

{

StringCollection domainList = new StringCollection();

try

{

DirectoryEntry en = new DirectoryEntry(';LDAP://';);

// Search for objectCategory type ';Domain';

DirectorySearcher srch = new DirectorySearcher(';objectCategory=Domain鈥?br>
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鈥?br>
}

}

}

catch (Exception ex)

{

Trace.Write(ex.Message);

}

return domainList;

}

No comments:

Post a Comment