Got this program i am writing in my office in C#.
So i can read all Computer Names From the Domain, fill them into a List and print them out.
Using this Code :
`private void FindComputersInAD()
{
List<string> computerNames = new List<string>();
DirectoryContext dirCtx = new DirectoryContext(DirectoryContextType.Domain, "domain.com");
using (Domain usersDomain = Domain.GetDomain(dirCtx))
using (DirectorySearcher adsearcher = new DirectorySearcher(usersDomain.GetDirectoryEntry()))
{
adsearcher.Filter = ("(objectCategory=computer)");
adsearcher.SizeLimit = 0;
adsearcher.PageSize = 250;
// Let searcher know which properties are going to be used, and only load those
adsearcher.PropertiesToLoad.Add("name");
foreach (SearchResult searchResult in adsearcher.FindAll())
{
if (searchResult.Properties["name"].Count > 0)
{
string computer = (string)searchResult.Properties["name"][0];
computerNames.Add(computer);
}
}
}
computerNames.Sort();
computerNames.ForEach(Console.WriteLine);
}`
Now that i got all the Computer Names, i would like to get their IP Addresses as well. But there is no such property.
I googled a lot and tried a few things but nothing works.
Can I use the DNS class to do this ?
I use this method to get the Host IP Address :
private void GetLocalIPAddress()
{
string _subString;
var _host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var _ip in _host.AddressList)
{
if (_ip.AddressFamily == AddressFamily.InterNetwork)
{
_subString = _ip.ToString().Substring(_ip.ToString().Length - 2);
//When a IP Address ends with .1, don't add it to the List.
if (_subString != ".1")
{
_hostIPAddress = _ip.ToString();
}
}
}
}
Can I use this also somehow to get the IP Addresses of the Computers ?
I would be very happy if you can help me :)
Thanks in advance
So i found myself an answer. Since all the computers are in the same domain i openend cmd and tried nslookup. And it returned the IP Address i wanted.
So i just built a C# DNSLookup Method, looks like this :
private void DNSLookup(string computerNameOrAddress)
{
IPHostEntry hostEntry = Dns.GetHostEntry(computerNameOrAddress);
IPAddress[] ips = hostEntry.AddressList;
foreach (IPAddress ip in ips)
{
Console.WriteLine(" Address: {0}", ip);
}
}
Anyway thanks for looking :)