
C# LocalAdapterInformation
As I continue to enhance some of my exisiting utilities (and port them to C#
) I found myself working on a utilitiy that determines the Local Network Adapter Information. As I had posted in the past (VB.NET – Delphi) here is the C# code for determining the information:
1 public void RetrieveLocalAdapterInformation(TextBox text)
2 {
3 NetworkInterface[] interfaces; // NIC
4 IPInterfaceProperties properties;
5 PhysicalAddress address;
6
7
8 text.Clear();
9
10 try
11 {
12 if (NetworkInterface.GetIsNetworkAvailable())
13 {
14 interfaces = NetworkInterface.GetAllNetworkInterfaces();
15 if (interfaces.GetLength(0) > 0)
16 {
17 foreach (NetworkInterface netInterface in interfaces)
18 {
19 properties = netInterface.GetIPProperties();
20
21 text.AppendText(netInterface.Name + Environment.NewLine);
22 text.AppendText("Id: " + netInterface.Id.ToString() + Environment.NewLine);
23 address = netInterface.GetPhysicalAddress();
24 text.AppendText("MAC Address: " +
25 (address.ToString() == String.Empty ? "None" : address.ToString()) +
26 Environment.NewLine);
27
28 foreach (IPAddressInformation ipinfo in properties.UnicastAddresses)
29 {
30 text.AppendText("IP: " + ipinfo.Address.ToString() +
31 Environment.NewLine);
32 }
33
34 foreach (IPAddress ip in properties.DhcpServerAddresses)
35 {
36 text.AppendText("DHCP Server: " + ip.ToString() +
37 Environment.NewLine);
38 }
39
40 foreach (IPAddress ip in properties.DnsAddresses)
41 {
42 text.AppendText("DNS Server: " + ip.ToString() + Environment.NewLine);
43 }
44
45 text.AppendText("Type: " + netInterface.NetworkInterfaceType.ToString() + Environment.NewLine);
46 text.AppendText("Operational Status: " + netInterface.OperationalStatus.ToString() + Environment.NewLine);
47 text.AppendText("Speed: " + netInterface.Speed.ToString("N") + " bytes" + Environment.NewLine);
48
49 // XP Only
50 System.OperatingSystem osInfo = System.Environment.OSVersion;
51 if ((osInfo.Version.Major == 5) & (osInfo.Version.Minor > 0))
52 {
53 text.AppendText("Receive Only: " +
54 netInterface.IsReceiveOnly.ToString() +
55 Environment.NewLine);
56 text.AppendText("Support Multicast: " +
57 netInterface.SupportsMulticast.ToString() +
58 Environment.NewLine);
59 }
60
61 text.AppendText("Support IPv4: " +
62 netInterface.Supports(NetworkInterfaceComponent.IPv4).ToString() +
63 Environment.NewLine);
64 text.AppendText("Support IPv6: " +
65 netInterface.Supports(NetworkInterfaceComponent.IPv6).ToString() +
66 Environment.NewLine);
67 text.AppendText("DnsSuffix: " +
68 properties.DnsSuffix.ToString() + Environment.NewLine);
69
70 text.AppendText(Environment.NewLine);
71 }
72 }
73 }
74 }
75 catch (Exception e)
76 {
77 MessageBox.Show(e.Message, e.Source, MessageBoxButtons.OK,
78 MessageBoxIcon.Error);
79 }
80 }
As usual a side note: I am always up for trying new things. After reading Steve’s post on a Code Syntax Highlighter; I used that tool for formatting the source code listed above.Labels: .NET, C#, Code