?
Solved

Visual C# - I need to get IP info

Posted on 2008-10-30
10
Medium Priority
?
1,308 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
[X]
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
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 1000 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
Quick Cloud Training

Looking for some quick training on the cloud in 2 hours or less? Check out these how-to guides in AWS, Linux, OpenStack, Azure, and more!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
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:…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 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