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> 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