C#獲取本機(jī)IP地址和Mac地址的方法
本文實(shí)例講述了C#獲取本機(jī)IP地址和Mac地址的方法。分享給大家供大家參考。具體分析如下:
查找了幾個(gè)方法,經(jīng)過調(diào)試修改,下面這個(gè)方法能很好的獲取到本地的IP和MAC地址??梢杂糜谶@方面的功能實(shí)現(xiàn)。主要是要添加System.Management的引用。
using System; using System.Management; using System.Net; public class Program { static void Main(string[] args) { try { string ip = ""; string mac = ""; ManagementClass mc; string hostInfo = Dns.GetHostName(); //IP地址 //System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;這個(gè)過時(shí) System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList; for (int i = 0; i < addressList.Length; i++) { ip = addressList[i].ToString(); } //mac地址 mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) { if (mo["IPEnabled"].ToString() == "True") { mac = mo["MacAddress"].ToString(); } } //輸出 string outPutStr = "IP:{0},\n MAC地址:{1}"; outPutStr = string.Format(outPutStr, ip, mac); Console.WriteLine(outPutStr); } catch (Exception e) { } Console.ReadLine(); } }
希望本文所述對大家的C#程序設(shè)計(jì)有所幫助。
相關(guān)文章
UnityShader使用圖像疊加實(shí)現(xiàn)運(yùn)動(dòng)模糊
這篇文章主要為大家詳細(xì)介紹了UnityShader使用圖像疊加實(shí)現(xiàn)運(yùn)動(dòng)模糊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02C#使用winform實(shí)現(xiàn)進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了C#使用winform實(shí)現(xiàn)進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07C#中的Timer和DispatcherTimer使用實(shí)例
這篇文章主要介紹了C#中的Timer和DispatcherTimer使用實(shí)例,本文分別給出它們的使用代碼實(shí)例,需要的朋友可以參考下2015-01-01C#實(shí)現(xiàn)根據(jù)指定容器和控件名字獲得控件的方法
這篇文章主要介紹了C#實(shí)現(xiàn)根據(jù)指定容器和控件名字獲得控件的方法,其中包括了遍歷與遞歸的應(yīng)用,需要的朋友可以參考下2014-08-08C#實(shí)現(xiàn)簡單學(xué)生成績管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡單學(xué)生成績管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C#簡單訪問SQLite數(shù)據(jù)庫的方法(安裝,連接,查詢等)
這篇文章主要介紹了C#簡單訪問SQLite數(shù)據(jù)庫的方法,涉及SQLite數(shù)據(jù)庫的下載、安裝及使用C#連接、查詢SQLIte數(shù)據(jù)庫的相關(guān)技巧,需要的朋友可以參考下2016-07-07

C#如何遠(yuǎn)程讀取服務(wù)器上的文本內(nèi)容