BPSoftware.com
Home   Utilities   Purchase   FAQ   Support   Contact        
Shareware Utilities
 APrintDirect
 AIconExtract
 AFile Attribute Manager
Freeware Utilities
 AddrMon
 AFileSync
 ASysIcon
 B&P Table Utilities
 BPACLer
 BPSNMPMon
 BPSNMPUtil
 CharCount
 Delphi® Components
 MacAddr
Miscellaneous
 BPSoftware Blog
 Purchase Shareware
 Support

 Subscribe!

Friday, February 02, 2007
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: , ,

posted by Brad Prendergast at 8:49:00 AM
Comments:
Links to this post:

Create a Link

Recent Posts
 PInvoke WIN32 and .NET
 ASP.NET upload a file HtmlInputFile Control
 PPCTL.DLL is damaged and could not be repaired.
 Google Mobile SMS
 Running a .NET Framework application from a networ...
 I am Green Lantern!
 More Hash - Part II
 More Hash - Part I
 Is it a Hash Brown?
 Virtualization is cool

 Subscribe!


Labels



Archives
 October 2005
 November 2005
 December 2005
 January 2006
 February 2006
 March 2006
 April 2006
 May 2006
 June 2006
 July 2006
 August 2006
 September 2006
 December 2006
 January 2007
 February 2007
 March 2007
 September 2007
 October 2007
 November 2007
 July 2008
 November 2008
Powered by Blogger