Solved

Visual C# - I need to get IP info

Posted on 2008-10-30
10
1,278 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
 

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now