C#獲取本機(jī)IP地址和Mac地址的方法
更新時間:2015年05月06日 11:50:15 作者:niuniu
這篇文章主要介紹了C#獲取本機(jī)IP地址和Mac地址的方法,實例分析了C#網(wǎng)絡(luò)功能的基本技巧,需要的朋友可以參考下
本文實例講述了C#獲取本機(jī)IP地址和Mac地址的方法。分享給大家供大家參考。具體分析如下:
查找了幾個方法,經(jīng)過調(diào)試修改,下面這個方法能很好的獲取到本地的IP和MAC地址。可以用于這方面的功能實現(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;這個過時
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è)計有所幫助。
相關(guān)文章
C# OleDbDataReader快速數(shù)據(jù)讀取方式(3種)
這篇文章主要介紹了C# OleDbDataReader快速數(shù)據(jù)讀取方式(3種),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
C# 4.0 大數(shù)的運算--BigInteger的應(yīng)用詳解
本篇文章是對C# 4.0 大數(shù)的運算 BigInteger的應(yīng)用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C#設(shè)置自定義文件圖標(biāo)實現(xiàn)雙擊啟動(修改注冊表)
這篇文章介紹的是利用C#設(shè)置自定義文件圖標(biāo),然后實現(xiàn)雙擊啟動的功能,文章給出了示例代碼,介紹的很詳細(xì),有需要的可以參考借鑒。2016-08-08
C#/VB.NET實現(xiàn)在Word文檔中添加頁眉和頁腳
頁眉位于文檔中每個頁面的頂部區(qū)域,常用于顯示文檔的附加信息;頁腳位于文檔中每個頁面的底部的區(qū)域,常用于顯示文檔的附加信息。今天這篇文章就將為大家展示如何以編程的方式在在?Word?文檔中添加頁眉和頁腳2023-03-03

