C#獲取計(jì)算機(jī)名,IP,MAC信息實(shí)現(xiàn)代碼
更新時(shí)間:2012年11月30日 10:58:47 作者:
利用C#獲取計(jì)算機(jī)名,IP,MAC信息如何實(shí)現(xiàn),一直是網(wǎng)友們的頭疼問題,本文整理了一些實(shí)現(xiàn)代碼,需要的朋友可以參考下
利用C#獲取計(jì)算機(jī)名,IP,MAC信息,如下為源代碼:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management;
namespace Wenanry.Net
{
/// <summary>
/// 獲取計(jì)算機(jī)系統(tǒng)信息
/// </summary>
public class ManagementSystemInfo
{
/// <summary>
/// 獲取主機(jī)名
/// </summary>
/// <returns></returns>
public string HostName
{
get
{
string hostname = Dns.GetHostName();
return hostname;
}
}
/// <summary>
/// 獲取IP地址
/// </summary>
/// <returns></returns>
public List<string> GetIPList()
{
List<string> ipList = new List<string>();
IPAddress[] addressList = Dns.GetHostEntry(this.HostName).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
ipList.Add(addressList[i].ToString());
}
return ipList;
}
/// <summary>
/// 獲取Mac地址
/// </summary>
/// <returns></returns>
public List<string> getMacList()
{
List<string> macList = new List<string>();
ManagementClass mc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
macList.Add(mo["MacAddress"].ToString());
}
return macList;
}
}
}
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Management;
namespace Wenanry.Net
{
/// <summary>
/// 獲取計(jì)算機(jī)系統(tǒng)信息
/// </summary>
public class ManagementSystemInfo
{
/// <summary>
/// 獲取主機(jī)名
/// </summary>
/// <returns></returns>
public string HostName
{
get
{
string hostname = Dns.GetHostName();
return hostname;
}
}
/// <summary>
/// 獲取IP地址
/// </summary>
/// <returns></returns>
public List<string> GetIPList()
{
List<string> ipList = new List<string>();
IPAddress[] addressList = Dns.GetHostEntry(this.HostName).AddressList;
for (int i = 0; i < addressList.Length; i++)
{
ipList.Add(addressList[i].ToString());
}
return ipList;
}
/// <summary>
/// 獲取Mac地址
/// </summary>
/// <returns></returns>
public List<string> getMacList()
{
List<string> macList = new List<string>();
ManagementClass mc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
macList.Add(mo["MacAddress"].ToString());
}
return macList;
}
}
}
相關(guān)文章
C# 中 System.Index 結(jié)構(gòu)體和 Hat 運(yùn)算符(^)的使用示例
這篇文章主要介紹了C# 中 System.Index 結(jié)構(gòu)體和 Hat 運(yùn)算符(^)的使用示例,幫助大家更好的理解和使用C#,感興趣的朋友可以了解下2020-09-09
C#實(shí)現(xiàn)gRPC服務(wù)和調(diào)用示例詳解
gRPC?是一種與語(yǔ)言無關(guān)的高性能遠(yuǎn)程過程調(diào)用?(RPC)?框架,這篇文章主要為大家詳細(xì)介紹了C#如何實(shí)現(xiàn)gRPC服務(wù)和調(diào)用,需要的可以參考一下2024-01-01
WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球
WPF的拖曳效果,基本配置一下,就可以了,但是自繪的話,就得自己控制。本文將利用WPF+SkiaSharp實(shí)現(xiàn)自繪拖曳小球,感興趣的可以動(dòng)手嘗試一下2022-07-07
使用C#9中records作為強(qiáng)類型ID的實(shí)例教程
這篇文章主要給大家介紹了關(guān)于使用C#9中records作為強(qiáng)類型ID的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

