C # Get native IP collection organize sample code sharing for 7 ways
①
1 private void GetIP()
2 {
3 string hostName = Dns.GetHostName();//The name of the machine
4 //System.Net.IPAddress[] addressList = Dns.GetHostByName(hostName).AddressList;// will warn that GetHostByName() has expired, I only returned an IPv4 address when I ran it.
5 System.Net.IPAddress[] addressList = Dns.GetHostAddresses(hostName);//will return all addresses, including IPv4 and IPv6
6 foreach (IPAddress ip in addressList)
7 {
8 listBox1.Items.Add(ip.ToString());
9 }
10 }
② using Iphostentry to get the native LAN address
1 static string GetLocalIp()
2 {
3 string hostname = Dns.GetHostName();//Get the native name
4 //IPHostEntry localhost = Dns.GetHostByName(hostname);//The method has expired, only the IPv4 address is obtained.
5 <SPAN style="WHITE-SPACE: pre"> </SPAN> IPHostEntry localhost = Dns.GetHostEntry(hostname);
6 IPAddress localaddr = localhost.AddressList[0];
7 return localaddr.ToString();
8 }
③ get the native network IP address
Method is sent by sending a WebRequest to some Web site that provides IP queries, and then analyzing the returned data stream
1 string strUrl = "link to the website providing the IP query";
2 Uri uri = new Uri(strUrl);
3 WebRequest webreq = WebRequest.Create(uri);
4 Stream s = webreq .GetResponse().GetResponseStream();
5 StreamReader sr = new StreamReader(s, Encoding.Default);
6 string all = sr.ReadToEnd();
7 int i = all.IndexOf("[") + 1;
8 / / Analysis string to get IP
9 return ip;
10 /* 11 I am using http://www.php.cn/
12 (This kind of link is easy to find, Baidu "IP" gets some websites, analyze the link of the website to get)
13 The data returned is:
14 <p class="well"><p>current IP:<code>0.0.0.0</code> from:XX City, XX Province, China Telecom</p><p>GeoIP: Beijing, China</p></p>
15 Analyze this paragraph
16 */
④//because of the use of ManagementClass, managementobjectcollection, must add the reference System.Management.dll and using System.Management;
1 private void GetIP2()
2 {
3 string stringMAC = "";
4 string stringIP = "";
5 ManagementClass managementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
6 ManagementObjectCollection managementObjectCollection = managementClass.GetInstances();
7 foreach(ManagementObject managementObject in managementObjectCollection)
8 {
9 if ((bool)managementObject["IPEnabled"] == true)
10 {
11 stringMAC += managementObject["MACAddress"].ToString();
12 string[] IPAddresses = (string[])managementObject["IPAddress"];
13 if (IPAddresses.Length > 0)
14 {
15 stringIP = IPAddresses[0];
16 }
17 }
18 }
19 txtMAC.Text = stringMAC.ToString();
20 txtIP.Text = stringIP.ToString();
21 }
⑤ invokes Web services provided by a Web site to query IP URLs
Get the long time, but did not learn how to invoke the Web Service, according to the search page do not do, then give up first ... After all, have not touched the webservice, another day to WebService fix again will be easy (left to improve it later)
⑥ obtains the IP by obtaining the result of the ipconfig command in CMD
1 private void GetIP6()
2 {
3 Process cmd = new Process();
4 cmd.StartInfo.FileName = "ipconfig.exe";//Set the program name 5 cmd.StartInfo.Arguments = "/all"; //Parameter
6 // Redirect standard output 7 cmd.StartInfo.RedirectStandardOutput = true;
8 cmd.StartInfo.RedirectStandardInput = true;
9 cmd.StartInfo.UseShellExecute = false;
10 cmd.StartInfo.CreateNoWindow = true;//Do not display the window (the console program is black)
11 //cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//I don’t understand what it means 12 /* 13 Collect it
14 About: How to display again after ProcessWindowStyle.Hidden is hidden?
15 hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName);
16 Win32Native.ShowWindow(hwndWin32Host, 1); //First FindWindow finds the window and then ShowWindow
17 */ 18 cmd.Start();
19 string info = cmd.StandardOutput.ReadToEnd();
20 cmd.WaitForExit();
21 cmd.Close();
22 textBox1.AppendText(info);
twenty three }
⑦networkinformation
1 private void GetIP5()
2 {
3 //The required namespace
4 //using System.Net.NetworkInformation;
5 //using System.Net.Sockets; 6 string str = "";
7 NetworkInterface[] adapters = NetworkInterface.GetAllNetworkInterfaces();
8 int i = 0;
9 foreach (NetworkInterface adapter in adapters)
10 {
11 12 IPInterfaceProperties adapterProperties = adapter.GetIPProperties();
13 UnicastIPAddressInformationCollection allAddress =
14 adapterProperties.UnicastAddresses;
15 if (allAddress.Count > 0)
16 {
17 str += "interface " + i + "description:\n\t " + adapter.Description + "\n ";
18 i++;
19 foreach (UnicastIPAddressInformation addr in allAddress)
20 {
21 if (addr.Address.AddressFamily == AddressFamily.InterNetworkV6)
twenty two {
23 ipListComb.Items.Add(addr.Address);
twenty four }
25 if (addr.Address.AddressFamily == AddressFamily.InterNetwork)
26 {
27 comboBox1.Items.Add(addr.Address);
28 }
29 30 }
31 }
32 }
33 MessageBox.Show(str);
34 }