C # get the native IP and wireless network IP

Source: Internet
Author: User
Tags get ip parse string

1 private void GetIP ()
2 {   3     string hostName = Dns.gethostname ();//native name    4     //system.net.ipaddress[] AddressList = Dns.gethostbyname (HostName). AddressList;
Warns that gethostbyname () has expired, I run and returns only one IPV4 address 5 system.net.ipaddress[] AddressList = dns.gethostaddresses ( HostName);
All addresses are returned, 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 native name   4             // Iphostentry localhost = dns.gethostbyname (hostname);
Method has expired, only to IPV4 address 5 <span style= "White-space:pre" > </SPAN>
Iphostentry localhost = dns.gethostentry (hostname); 6 IPAddress localaddr = localhost. ADDRESSLIST[0]; 7 return LOCALADDR. ToString (); 8 }

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 = "links to websites that provide IP queries";   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        //parse string to get IP    9        return IP;  Ten        *         /One I use Http://www.ip.cn/getip.php?action=getip&ip_url=&from=web         (this link is easy to find , Baidu "IP" to get some sites, analysis of the site links can be obtained)         the data returned is:         <div class= "Well" ><p> current ip:<code> 0.0.0.0</code>&nbsp;
From: xx province xx City Telecom </p><p>geoip:beijing, china</p></div> Analysis This paragraph on the line of * *

Get the IP by getting 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 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; Ten cmd. Startinfo.createnowindow = true;//does not show the window (console program is black screen) one//cmd. Startinfo.windowstyle = Processwindowstyle.hidden; 
//Do not understand what the meaning of 12/* 13 to collect the preparedness 14 about: Processwindowstyle.hidden hidden after how to display? Hwndwin32host = Win32native.findwindow (null, win32exinfo.windowsname); Win32native.showwindow (Hwndwin32host, 1);
//FindWindow Find the window before ShowWindow +/-cmd. Start (); string info = cmd. Standardoutput.readtoend (); CMD. WaitForExit (); + cmd. Close (); Textbox1.appendtext (info); +}

C # Gets the native IP and the wireless network IP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.