C # get the native IP

Source: Internet
Author: User
Tags get ip parse string

Method One:

private void GetIP ()
{
String hostName = Dns.gethostname ();//native name
system.net.ipaddress[] AddressList = Dns.gethostbyname (hostName). Addresslist;//warns that gethostbyname () is out of date, I run and returns only one IPV4 address
system.net.ipaddress[] AddressList = dns.gethostaddresses (hostName);//returns all addresses, including IPV4 and IPV6
foreach (IPAddress IP in AddressList)
{
LISTBOX1.ITEMS.ADD (IP. ToString ());
}
}

Method Two: Use Iphostentry to get the native LAN address

static string Getlocalip ()
{
string hostname = Dns.gethostname ();//Get native name
Iphostentry localhost = dns.gethostbyname (hostname);//method has expired, only to IPV4 address
<span style= "White-space:pre" > </SPAN> iphostentry localhost = dns.gethostentry (hostname);
IPAddress localaddr = localhost. ADDRESSLIST[0];
Return LOCALADDR. ToString ();
}

Method Three: Send WebRequest to some Web sites that provide IP queries, and then analyze the returned data streams

String strURL = "links to websites that provide IP queries";
Uri uri = new Uri (strURL);
WebRequest webreq = WebRequest.Create (URI);
Stream s = webreq. GetResponse (). GetResponseStream ();
StreamReader sr = new StreamReader (s, Encoding.default);
String all = Sr. ReadToEnd ();
int i = all. IndexOf ("[") + 1;
Parse string to get IP
return IP;
/*
I'm using a 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 are:
<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>
Just parse this section.
*/

Method Four: Obtain the IP by obtaining the result of the ipconfig command in CMD

private void GetIP6 ()
{
Process cmd = new process ();
Cmd. Startinfo.filename = "Ipconfig.exe";//Set Program name
Cmd. startinfo.arguments = "/all"; Parameters
REDIRECT Standard output
Cmd. Startinfo.redirectstandardoutput = true;
Cmd. Startinfo.redirectstandardinput = true;
Cmd. Startinfo.useshellexecute = false;
Cmd. Startinfo.createnowindow = true;//does not display window (console program is black screen)
Cmd. Startinfo.windowstyle = processwindowstyle.hidden;//don't understand what it means
/*
Gather a little bit of precaution
About: How to display Processwindowstyle.hidden after hiding?
Hwndwin32host = Win32native.findwindow (null, win32exinfo.windowsname);
Win32native.showwindow (Hwndwin32host, 1); First FindWindow Find the window and then ShowWindow
*/
Cmd. Start ();
string info = cmd. Standardoutput.readtoend ();
Cmd. WaitForExit ();
Cmd. Close ();
Textbox1.appendtext (info);
}

C # get the native IP

Related Article

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.