I know this is an old topic, but I wanted to post the final version of my code. Special thanks to
anvas kuttan[^][
for his input - it was greatly appreciated! This was also the first real C# program that I've written (outside of Hello World!). I'm not a good programmer by any means (no matter what my BS in Computer Science says LOL). I really like C# and hope to expand my knowledge of it.
using System;
using System.IO;
using System.Management;
namespace SysInfo
{
class Program
{
static void Main(string[] args)
{
string ipaddress = null;
string hostname = null;
StreamWriter hostwrite = new StreamWriter(@"c:\hostname.txt ", false);
StreamWriter ipwrite = new StreamWriter(@"c:\ipaddress.txt", false);
string query = "SELECT * FROM Win32_NetworkAdapterConfiguration" + " WHERE Description LIKE 'BROAD%'";
ManagementObjectSearcher moSearch = new ManagementObjectSearcher(query);
ManagementObjectCollection moCollection = moSearch.Get();
foreach (ManagementObject mo in moCollection)
{
hostname = mo["DNSHostName"].ToString();
hostwrite.WriteLine(hostname);
string[] addresses = (string[])mo["IPAddress"];
foreach (string ip in addresses)
{
ipaddress = ip;
ipwrite.WriteLine(ipaddress);
}
}
Console.WriteLine("If you see this window, you can close it");
hostwrite.Close();
ipwrite.Close();
}
}
}