• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 497
  • Last Modified:

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

Does anyone have sample code to get Ip Address and Subnet  in c# or vb.net for vs2005
0
jr3
Asked:
jr3
  • 5
  • 4
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
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

0
 
jr3Author 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?
0
 
Carl TawnSystems and Integration DeveloperCommented:
If you have a using directive at the top of your file then you shouldn't need the full reference in the code.
0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
jr3Author 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?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
jr3Author 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?
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
jr3Author 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.
0
 
Carl TawnSystems and Integration DeveloperCommented:
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
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now