“Ipconfig.exe/all "can only obtain internal IP addresses in the LAN, and cannot obtain external IP addresses of the local machine, as does DNS. Resolve.
After research, in the LAN, only by the power of the local machine is unable to obtain the external IP of the local machine, you must log on to a server on the Internet, that server will know your IP address, and then you get it back.
1 Using System;
2 Using System. IO;
3 Using System. net;
4 Using System. Text. regularexpressions;
5
6 Class Test
7 {
8 // Obtain the HTML content of a webpage
9 Static String Getpage ( String URL)
10 {
11 Httpwebrequest req = (Httpwebrequest) webrequest. Create (URL );
12 Try
13 {
14 Using (Httpwebresponse res = (Httpwebresponse) Req. getresponse ())
15 {
16 Using (Streamreader SR = New Streamreader (res. getresponsestream ()))
17 {
18ReturnSr. readtoend ();
19}
20 }
21 }
22 Catch (System. Exception E)
23 {
24ReturnE. message;
25}
26 Finally
27 {
28Req. Abort ();
29}
30 }
31
32 // Obtain the external IP address of the local machine through an external website
33 Static String Getouterip ()
34 {
35 String Patt = @" IP :\[(? <Ip> [0-9 \.] *) \] " ;
36 String URL = " Http://www.skyiv.com/info " ;
37 Return RegEx. Match (getpage (URL), patt). Groups [ " IP " ]. Value;
38 }
39
40 Static Void Main ()
41 {
42 Console. writeline ( " Inner IP: " );
43 Foreach (IPaddress IP In DNS. Resolve (DNS. gethostname (). Addresslist)
44 {
45Console. writeline (IP );
46}
47 Console. writeline ();
48 Console. writeline ( " Outer IP: " );
49 Console. writeline (getouterip ());
50 }
51 }