C#獲取本地IP的四種方式示例詳解
1.第一種方式
采用System.Net.Dns的GetHostAddress的方式,具體請看代碼:
/// <summary> /// 網(wǎng)絡不通暢可以獲取 /// 不過能獲取到具體的IP /// </summary> /// <returns></returns> public static List<IPAddress> GetByGetHostAddresses() { try { IPAddress[] adds = Dns.GetHostAddresses(Dns.GetHostName()); return adds == null || adds.Length == 0 ? new List<IPAddress>() : adds.ToList<IPAddress>(); } catch (Exception) { return new List<IPAddress>(); } }
這種方式受到網(wǎng)絡的影響,如果沒有連接到網(wǎng)絡,本地配置的部分IP是獲取不到的,我也遇到一種情況是,電腦環(huán)境正常,就是獲取不到,原因至今還不知道;
2.第二種方式
采用System.Management.ManagementClass來獲取,詳細請看代碼:
/// <summary> /// 只有網(wǎng)絡通暢才能獲取 /// </summary> /// <returns></returns> public static List<IPAddress> GetByManagementClass() { try { ManagementClass mClass = new System.Management.ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection managementObjectCollection = mClass.GetInstances(); List<IPAddress> ls = new List<IPAddress>(); foreach (var item in managementObjectCollection) { if ((bool)item["IPEnabled"] == true) { foreach (var ip in (string[])item["IPAddress"]) { IPAddress ipout = null; IPAddress.TryParse(ip, out ipout); if (ipout != null) { ls.Add(ipout); } } } } return ls; } catch (Exception) { return new List<IPAddress>(); } }
同樣的這種方式也受到網(wǎng)絡的約束,沒有聯(lián)網(wǎng)的狀態(tài)下不一定能夠獲取到IP;
3.第三種方式
我們平時在命令行中輸入ipconfig命令同樣也是能獲取,在程序中啟動Ipconfig應用程序,然后解析出來,也是可以獲取得到IP,詳細請看代碼:
public static List<IPAddress> GetByCMD() { try { Process cmd = new Process(); cmd.StartInfo.FileName = "ipconfig.exe"; cmd.StartInfo.Arguments = "/all"; cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.RedirectStandardInput = true; cmd.StartInfo.UseShellExecute = false; cmd.StartInfo.CreateNoWindow = true; cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; cmd.Start(); string info = ""; List<IPAddress> ls = new List<IPAddress>(); // info = cmd.StandardOutput.ReadToEnd(); Regex validipregex = new Regex(@"\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}"); //new Regex(@"^(([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([1-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"); while ((info = cmd.StandardOutput.ReadLine()) != null) { IPAddress ip = null; Console.WriteLine(info); info = validipregex.Match(info).Value; IPAddress.TryParse(info, out ip); if (ip != null) { ls.Add(ip); } } cmd.WaitForExit(); cmd.Close(); return ls; } catch (Exception) { return new List<IPAddress>(); } }
即便是通過這種方式來獲取IP,如果在本機電腦沒有聯(lián)網(wǎng)的狀態(tài)下,也是獲取不到IP的,并且也不太建議使用這種方式;
4.第四種方法
采用NetworkInterface.GetAllNetworkInterfaces的方式是不受網(wǎng)絡的影響的,聯(lián)網(wǎng)或者不聯(lián)網(wǎng)都能夠獲取到IP,詳細請看代碼:
/// <summary> /// 無論網(wǎng)絡通不通都能獲取到Ip /// </summary> /// <returns></returns> public static List<IPAddress> GetByNetworkInterface() { try { NetworkInterface[] intf = NetworkInterface.GetAllNetworkInterfaces(); List<IPAddress> ls = new List<IPAddress>(); foreach (var item in intf) { IPInterfaceProperties adapterPropertis = item.GetIPProperties(); UnicastIPAddressInformationCollection coll = adapterPropertis.UnicastAddresses; foreach (var col in coll) { ls.Add(col.Address); } } return ls; } catch (Exception) { return new List<IPAddress>(); } }
以上所說的聯(lián)網(wǎng),包括連接在局域網(wǎng)中。
希望給有需要的朋友們帶來幫助;
到此這篇關于C#獲取本地IP的四種方式示例詳解的文章就介紹到這了,更多相關C#獲取本地IP內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
- C# 使用multipart form-data方式post數(shù)據(jù)到服務器
- C#監(jiān)測IPv4v6網(wǎng)速及流量的實例代碼
- c#獲取客戶端IP地址(考慮代理)
- C#應用ToolStrip控件使用方法
- C#使用GZipStream實現(xiàn)文件的壓縮與解壓
- C# 使用SharpZipLib生成壓縮包的實例代碼
- C#自定義IP輸入框控件
- c# 文件壓縮zip或將zip文件解壓的方法
- C#多線程爬蟲抓取免費代理IP的示例代碼
- C# JavaScriptSerializer序列化時的時間處理詳解
- 使用C# 的webBrowser寫模擬器時的javascript腳本調(diào)用問題
- C#實現(xiàn)文件壓縮與解壓的方法示例【ZIP格式】
- c# 獲得本地ip地址的三種方法
相關文章
C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析
這篇文章主要介紹了C#中Forms.Timer、Timers.Timer、Threading.Timer的用法分析,以實例形式較為詳細的講述了.NET Framework里面提供的三種Timer具體用法,需要的朋友可以參考下2014-10-10C#裝飾器模式(Decorator Pattern)實例教程
這篇文章主要介紹了C#裝飾器模式(Decorator Pattern),以一個完整實例形式講述了C#裝飾器模式的實現(xiàn)過程,有助于深入理解C#程序設計思想,需要的朋友可以參考下2014-09-09DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法
這篇文章主要介紹了DevExpress實現(xiàn)禁用TreeListNode CheckBox的方法,在項目開發(fā)中有應用價值,需要的朋友可以參考下2014-08-08