Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Visual C# - I need to get IP info

Posted on 2008-10-30
10
1,291 Views
Last Modified: 2013-12-17
I'm using the following code to show NIC adapter info... but I Need to also list the IP for each found card. What do I need to change?


In Form_Load I have the following:
            dataGridView1.Columns.Add("IP", "IP Address");
            dataGridView1.Columns.Add("MAC", "Mac Address");
            dataGridView1.Columns.Add("Type", "Type");
            dataGridView1.Columns.Add("Name", "Name");

And then I have a button that does the following:
            private void button1_Click(object sender, EventArgs e)
            {
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
                    string[] mac = { ???Here is where I would put the IP info???????, adapter.GetPhysicalAddress().ToString(), adapter.NetworkInterfaceType.ToString(), adapter.Description };
                    dataGridView1.Rows.Add(mac);

                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        this.dataGridView1.AutoResizeRow(i);
                    }
                }
            }
0
Comment
Question by:MiSheps
10 Comments
 
LVL 53

Expert Comment

by:Dhaest
ID: 22838748
0
 

Expert Comment

by:jman0
ID: 22838755
You can find a small C# project that achieves exactly what you need at

http://www.codeproject.com/KB/IP/Net_Adapter_Info_in_C_.aspx
0
 
LVL 1

Assisted Solution

by:Aleksey_Shturman
Aleksey_Shturman earned 250 total points
ID: 22838760
For your case - I recomend to use the following code

private void button1_Click(object sender, EventArgs e)
            {
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
IPAddress ipLocal = ( adapter.GetIPProperties().UnicastAddresses != null && adapter.GetIPProperties().UnicastAddresses.Count > 0 ? adapter.GetIPProperties().UnicastAddresses[0].Address : null);
                    string[] mac = { ipLocal.ToString() , adapter.GetPhysicalAddress().ToString(), adapter.NetworkInterfaceType.ToString(), adapter.Description };
                    dataGridView1.Rows.Add(mac);
 
                    for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                    {
                        this.dataGridView1.AutoResizeRow(i);
                    }
                }
            }

Open in new window

0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:MiSheps
ID: 22844181
Aleksey,

When I run your code, I get a debug error:

The type or namespace name 'IPAddress' could not be found (are you missing a using directive or an assemply reference?)
0
 

Author Comment

by:MiSheps
ID: 22844190
nevermind, I didn't have a using system.net;

Testing now...
0
 

Author Comment

by:MiSheps
ID: 22844423
ok, tested the code... and it works perfect.... as long as ALL ADAPTERS have IP addresses.

But, if one, or more of them don't, it bugs out.

This is almost perfect, but if it finds an adapter that doesn't have an IP, I need it to show it as 0.0.0.0 and not bug/crash.

Any thoughts?
0
 

Accepted Solution

by:
MiSheps earned 0 total points
ID: 22845008
Ah-Hah! Fixed it.

I added a check for null value to handle the error and then parced the 0.0.0.0 into ipaddress type, and whaaalahhh... resulted in exactly what I want...

This is my favorite part of coding,... a clean compile! lol

Here is the end result code just for the record.

//##################
//### In Form Load ####
//##################
            dataGridView1.Columns.Add("IP", "IP Address");
            dataGridView1.Columns.Add("Name", "Name");
            dataGridView1.Columns.Add("Type", "Type");
            dataGridView1.Columns.Add("MAC", "Mac Address");

//##################
//### Button 1        ####
//##################
        private void button1_Click(object sender, EventArgs e)
            {
                dataGridView1.Rows.Clear();
                NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
                foreach (NetworkInterface adapter in nics)
                {
                            IPAddress ipLocal = (adapter.GetIPProperties().UnicastAddresses != null && adapter.GetIPProperties().UnicastAddresses.Count > 0 ? adapter.GetIPProperties().UnicastAddresses[0].Address : null);

                            if (ipLocal == null)
                            {
                                ipLocal = IPAddress.Parse("0.0.0.0");
                            }
                            string[] mac = { ipLocal.ToString(), adapter.Description, adapter.NetworkInterfaceType.ToString(), adapter.GetPhysicalAddress().ToString() };
                            dataGridView1.Rows.Add(mac);

                            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
                            {
                                this.dataGridView1.AutoResizeRow(i);
                            }
                }
            }
0
 

Author Comment

by:MiSheps
ID: 22845076
Aleksey, I'm giving you credit, because it was your code the helped the most. Thank you.

However, for anyone who may stumble into this thread, the solution that I just posted is the complete solution with the proper error handling of an adapter without an address.

Dhaest: I don't like your solution because it assums that DNS is available, and that the IP that is on the system is dished. From reading around, this will NOT work 100% of the time, ie in dial up situations, static situations, etc.

jman0: Your solution looks like it would work well enough, but I really don't want to have to poke into the registry if I don't have to. Plus, the code is a bit big and bulky.

Aleksey: Right on target. Good clean code, that works well (minus the small bug of not handling a null value well).

Thank you to everyone for helping! I appreciate your effort!

0
 
LVL 53

Expert Comment

by:Dhaest
ID: 22845519
You can close the question by yourself ... you don't need the support of the moderators
http://www.experts-exchange.com/help.jsp#hi407
0
 

Author Comment

by:MiSheps
ID: 22845587
I did close it DHaest,

I used:

Accept your own comment, and award points to Experts for their assistance
If you answered your own question, but wish to award points to Experts for their attempts to help, use this option.

And when I did that, it automatically went into a request status.

Thanks for the info anyway though.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question