Solved

Visual C# - I need to get IP info

Posted on 2008-10-30
10
1,294 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

730 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