We help IT Professionals succeed at work.

Does anyone have sample code to get Ip Address and Subnet in c# or vb.net for vs2005

jr3
jr3 asked
on
Medium Priority
608 Views
Last Modified: 2013-11-26
Does anyone have sample code to get Ip Address and Subnet  in c# or vb.net for vs2005
Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:
You can try the following, which should give you all of the network adapters installed on the local machine and their subnet:
            foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            {
                Console.WriteLine(ni.Name);
 
                IPInterfaceProperties ip = ni.GetIPProperties();
                foreach (UnicastIPAddressInformation unicast in ip.UnicastAddresses)
                {
                    Console.WriteLine(unicast.Address.ToString());
                    Console.WriteLine(unicast.IPv4Mask == null ? "No subnet" : unicast.IPv4Mask.ToString());
                    Console.WriteLine();
                }
            }  

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
jr3

Author

Commented:
foreach (System.Net.NetworkInformation.NetworkInterface ni in System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces())


            {
                Console.WriteLine(ni.Name);

                System.Net.NetworkInformation.IPInterfaceProperties ip = ni.GetIPProperties();
                foreach (System.Net.NetworkInformation.UnicastIPAddressInformation unicast in ip.UnicastAddresses)
                {
                    Console.WriteLine(unicast.Address.ToString());
                    Console.WriteLine(unicast.IPv4Mask == null ? "No subnet" : unicast.IPv4Mask.ToString());
                    Console.WriteLine();
                }
            }  

This worked I am very excited but can you tell me why I had to type          System.Net.NetworkInformation.IPInterfaceProperties  even though I had the "using System.Net.NetworkInformation;" line at the top?
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
If you have a using directive at the top of your file then you shouldn't need the full reference in the code.
jr3

Author

Commented:
I am using VS 2005. It is my understanding that system.net is not a core reference in 2005 but it is in 2008. The using statement only works with core refernce. Do you know if that is true?
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
I think it depends on the type of project you create as to what assemblies are referenced by default. But System.Net is definately one of the defaults for WinForms and Console projects.
jr3

Author

Commented:
What do you mean by type of project? This is c# in vs2005. When I type system .net in the code it works but when I right click on the project in solution explorer and click on "add reference" I do not see it under the .net tab?
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
It won't be because it is already referenced in the project. If the assembly wasn't referenced then intellisense wouldn't recognise System.Net either.
jr3

Author

Commented:
Yeah that is what I thought. My question is when I right click on the project in solution explorer and click on "add reference" I do not see it under the .net  tab and never have. How did system.net get refernced.
Carl TawnSenior Systems and Integration Developer
CERTIFIED EXPERT

Commented:
Simply because there is no System.Net.dll in .Net 2. The System.Net namespace is contained with System.dll which is added to all projects by default in VS2005
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.