獲取本機所有IP地址:
這些地址是包含所有網(wǎng)卡(虛擬網(wǎng)卡)的ipv4和ipv6地址。
1
2
|
string name = Dns.GetHostName(); IPAddress[] ipadrlist = Dns.GetHostAddresses(name); |
獲取本機所有IPV4地址:
1
2
3
4
5
6
7
|
string name = Dns.GetHostName(); IPAddress[] ipadrlist = Dns.GetHostAddresses(name); foreach (IPAddress ipa in ipadrlist) { if (ipa.AddressFamily == AddressFamily.InterNetwork) Console.Writeline(ipa.ToString()); } |
若要單單獲取ipv4地址,可以用IPAdress.AddressFamily 屬性判斷:對于 IPv4,返回 InterNetwork;對于 IPv6,返回 InterNetworkV6。
然而如果本機可能有多個ipv4的地址,那如何獲取訪問默認(rèn)網(wǎng)關(guān)時使用的網(wǎng)卡IP呢。在CSDN論壇找到了大神的方法,用的是查詢本機路由表。
獲取本機正在使用的ipv4地址(訪問互聯(lián)網(wǎng)的IP)
可別小看,還是有很多需要考慮的:
1.一個電腦有多個網(wǎng)卡,有線的、無線的、還有vmare虛擬的兩個網(wǎng)卡。
2.就算只有一個網(wǎng)卡,但是該網(wǎng)卡配置了N個IP地址.其中還包括ipv6地址。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
/// <summary> /// 獲取當(dāng)前使用的IP /// </summary> /// <returns></returns> public static string GetLocalIP() { string result = RunApp( "route" , "print" , true ); Match m = Regex.Match(result, @"0.0.0.0\s+0.0.0.0\s+(\d+.\d+.\d+.\d+)\s+(\d+.\d+.\d+.\d+)" ); if (m.Success) { return m.Groups[2].Value; } else { try { System.Net.Sockets.TcpClient c = new System.Net.Sockets.TcpClient(); c.Connect( "www.baidu.com" , 80); string ip = ((System.Net.IPEndPoint)c.Client.LocalEndPoint).Address.ToString(); c.Close(); return ip; } catch (Exception) { return null ; } } } /// <summary> /// 獲取本機主DNS /// </summary> /// <returns></returns> public static string GetPrimaryDNS() { string result = RunApp( "nslookup" , "" , true ); Match m = Regex.Match(result, @"\d+\.\d+\.\d+\.\d+" ); if (m.Success) { return m.Value; } else { return null ; } } /// <summary> /// 運行一個控制臺程序并返回其輸出參數(shù)。 /// </summary> /// <param name="filename">程序名</param> /// <param name="arguments">輸入?yún)?shù)</param> /// <returns></returns> public static string RunApp( string filename, string arguments, bool recordLog) { try { if (recordLog) { Trace.WriteLine(filename + " " + arguments); } Process proc = new Process(); proc.StartInfo.FileName = filename; proc.StartInfo.CreateNoWindow = true ; proc.StartInfo.Arguments = arguments; proc.StartInfo.RedirectStandardOutput = true ; proc.StartInfo.UseShellExecute = false ; proc.Start(); using (System.IO.StreamReader sr = new System.IO.StreamReader(proc.StandardOutput.BaseStream, Encoding.Default)) { //string txt = sr.ReadToEnd(); //sr.Close(); //if (recordLog) //{ // Trace.WriteLine(txt); //} //if (!proc.HasExited) //{ // proc.Kill(); //} //上面標(biāo)記的是原文,下面是我自己調(diào)試錯誤后自行修改的 Thread.Sleep(100); //貌似調(diào)用系統(tǒng)的nslookup還未返回數(shù)據(jù)或者數(shù)據(jù)未編碼完成,程序就已經(jīng)跳過直接執(zhí)行 //txt = sr.ReadToEnd()了,導(dǎo)致返回的數(shù)據(jù)為空,故睡眠令硬件反應(yīng) if (!proc.HasExited) //在無參數(shù)調(diào)用nslookup后,可以繼續(xù)輸入命令繼續(xù)操作,如果進程未停止就直接執(zhí)行 { //txt = sr.ReadToEnd()程序就在等待輸入,而且又無法輸入,直接掐住無法繼續(xù)運行 proc.Kill(); } string txt = sr.ReadToEnd(); sr.Close(); if (recordLog) Trace.WriteLine(txt); return txt; } } catch (Exception ex) { Trace.WriteLine(ex); return ex.Message; } } |
另有一種方法通過用ipconfig來獲取:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
private void GetIP() { Process cmd = new Process(); cmd.StartInfo.FileName = "ipconfig.exe" ; //設(shè)置程序名 cmd.StartInfo.Arguments = "/all" ; //參數(shù) //重定向標(biāo)準(zhǔn)輸出 cmd.StartInfo.RedirectStandardOutput = true ; cmd.StartInfo.RedirectStandardInput = true ; cmd.StartInfo.UseShellExecute = false ; cmd.StartInfo.CreateNoWindow = true ; //不顯示窗口(控制臺程序是黑屏) //cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//暫時不明白什么意思 /* 收集一下 有備無患 關(guān)于:ProcessWindowStyle.Hidden隱藏后如何再顯示? hwndWin32Host = Win32Native.FindWindow(null, win32Exinfo.windowsName); Win32Native.ShowWindow(hwndWin32Host, 1); //先FindWindow找到窗口后再ShowWindow */ cmd.Start(); string info = cmd.StandardOutput.ReadToEnd(); cmd.WaitForExit(); cmd.Close(); textBox1.AppendText(info); } |
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時也希望多多支持服務(wù)器之家!
原文鏈接:http://www.cnblogs.com/lijianda/p/6604651.html