Solved

Visual C# - I need to get IP info

Posted on 2008-10-30
10
1,273 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
Comment Utility
0
 

Expert Comment

by:jman0
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
nevermind, I didn't have a using system.net;

Testing now...
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:MiSheps
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

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…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

18 Experts available now in Live!

Get 1:1 Help Now