日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#获取电脑IP、MAC地址示例代码

發(fā)布時間:2023/12/10 C# 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#获取电脑IP、MAC地址示例代码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.


? ??

? ? /// <summary>

? ? /// 使用 C# 自帶的類庫實現(xiàn)計算機信息獲取

? ? /// </summary>

? ? public class DefaultDeviceInfo?

? ? {

? ? ? ? public virtual string GetCpuId()

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? string cpuInfo = " ";

? ? ? ? ? ? ? ? ManagementClass cimobject = new ManagementClass("Win32_Processor");

? ? ? ? ? ? ? ? ManagementObjectCollection moc = cimobject.GetInstances();

? ? ? ? ? ? ? ? foreach (ManagementObject mo in moc)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? cpuInfo = mo.Properties["ProcessorId"].Value.ToString();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? return cpuInfo.ToString();

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? {


? ? ? ? ? ? }


? ? ? ? ? ? return string.Empty;

? ? ? ? }


? ? ? ? public virtual string GetHDid()

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? string HDid = " ";

? ? ? ? ? ? ? ? ManagementClass cimobject1 = new ManagementClass("Win32_DiskDrive");

? ? ? ? ? ? ? ? ManagementObjectCollection moc1 = cimobject1.GetInstances();

? ? ? ? ? ? ? ? foreach (ManagementObject mo in moc1)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? HDid = (string)mo.Properties["Model"].Value;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? return HDid.ToString();

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? {

? ? ? ? ? ? }

? ? ? ? ? ? return string.Empty;

? ? ? ? }


? ? ? ? public virtual string GetIpv4()

? ? ? ? {

? ? ? ? ? ? string ip = GetIpv4("127.0.0.1");

? ? ? ? ? ? //如果根據(jù)127.0.0.1無法獲取ip地址,則嘗試根據(jù)主機名獲取

? ? ? ? ? ? if (ip == IPAddress.None.ToString())

? ? ? ? ? ? {

? ? ? ? ? ? ? ? ip = GetIpv4(null);

? ? ? ? ? ? }


? ? ? ? ? ? return ip;

? ? ? ? }


? ? ? ? public virtual string GetMac()

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? List<string> macs = new List<string>();

? ? ? ? ? ? ? ? NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();

? ? ? ? ? ? ? ? foreach (NetworkInterface ni in interfaces)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? if (ni.NetworkInterfaceType != NetworkInterfaceType.Ethernet)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? if (ni.GetPhysicalAddress().ToString() != "")

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? macs.Add(ni.GetPhysicalAddress().ToString());

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? //替補mac地址,當找不到以太網(wǎng)mac,則使用第一個mac

? ? ? ? ? ? ? ? var subs = macs.Count == 0 && interfaces.Length > 0

? ? ? ? ? ? ? ? ? ? ? interfaces[0].GetPhysicalAddress().ToString()

? ? ? ? ? ? ? ? ? ? : string.Empty;


? ? ? ? ? ? ? ? return macs.Count > 0 ? macs[0] : subs;

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception)

? ? ? ? ? ? {


? ? ? ? ? ? }


? ? ? ? ? ? return string.Empty;

? ? ? ? }


? ? ? ? public virtual string GetHostName()

? ? ? ? {

? ? ? ? ? ? return Dns.GetHostName();

? ? ? ? }


? ? ? ? #region 內(nèi)部方法


? ? ? ? /// <summary>

? ? ? ? /// 字符串每隔多少位插入特定字符

? ? ? ? /// </summary>

? ? ? ? /// <param name="value"></param>

? ? ? ? /// <param name="separator"></param>

? ? ? ? /// <param name="separatorLen"></param>

? ? ? ? /// <returns></returns>

? ? ? ? protected static string StringInsertSeparator(string value, string separator, int separatorLen = 1)

? ? ? ? {

? ? ? ? ? ? if (string.IsNullOrWhiteSpace(value))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? return value;

? ? ? ? ? ? }


? ? ? ? ? ? StringBuilder sbNewValue = new StringBuilder();


? ? ? ? ? ? for (int i = 0; i < value.ToArray().Count(); i++)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if (i > 0 && i % separatorLen == 0)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? sbNewValue.Append(separator);

? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? sbNewValue.Append(value[i]);

? ? ? ? ? ? }


? ? ? ? ? ? return sbNewValue.ToString();

? ? ? ? }


? ? ? ? /// <summary>

? ? ? ? /// 根據(jù)指定的主機名獲取ip地址

? ? ? ? /// 如果主機名為空,則獲取系統(tǒng)主機名稱

? ? ? ? /// </summary>

? ? ? ? /// <param name="defHostName"></param>

? ? ? ? /// <returns></returns>

? ? ? ? protected string GetIpv4(string defHostName)

? ? ? ? {

? ? ? ? ? ? try

? ? ? ? ? ? {

? ? ? ? ? ? ? ? /* NetworkInterface 實現(xiàn)方法

? ? ? ? ? ? ? ? // 獲得網(wǎng)絡(luò)接口,網(wǎng)卡,撥號器,適配器都會有一個網(wǎng)絡(luò)接口?

? ? ? ? ? ? ? ? NetworkInterface[] networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();


? ? ? ? ? ? ? ? foreach (NetworkInterface network in networkInterfaces)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? if (network.NetworkInterfaceType != NetworkInterfaceType.Ethernet)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? // 獲得當前網(wǎng)絡(luò)接口屬性

? ? ? ? ? ? ? ? ? ? IPInterfaceProperties properties = network.GetIPProperties();


? ? ? ? ? ? ? ? ? ? // 每個網(wǎng)絡(luò)接口可能會有多個IP地址?

? ? ? ? ? ? ? ? ? ? foreach (IPAddressInformation address in properties.UnicastAddresses)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? // 如果此IP不是ipv4,則進行下一次循環(huán)

? ? ? ? ? ? ? ? ? ? ? ? if (address.Address.AddressFamily != AddressFamily.InterNetwork)

? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? ? ? // 忽略127.0.0.1

? ? ? ? ? ? ? ? ? ? ? ? if (IPAddress.IsLoopback(address.Address))

? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? ? ? return address.Address.ToString();

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? */


? ? ? ? ? ? ? ? string hostName = defHostName;

? ? ? ? ? ? ? ? if (string.IsNullOrWhiteSpace(hostName))

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? hostName = Dns.GetHostName();

? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? var ipList = Dns.GetHostEntry(hostName).AddressList;


? ? ? ? ? ? ? ? foreach (var address in ipList)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? // 如果此IP不是ipv4,則進行下一次循環(huán)

? ? ? ? ? ? ? ? ? ? if (address.AddressFamily != AddressFamily.InterNetwork)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? // 忽略127.0.0.1

? ? ? ? ? ? ? ? ? ? if (IPAddress.IsLoopback(address))

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? continue;

? ? ? ? ? ? ? ? ? ? }


? ? ? ? ? ? ? ? ? ? return address.ToString();

? ? ? ? ? ? ? ? }


? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception ex)

? ? ? ? ? ? {

? ? ? ? ? ? ? ??

? ? ? ? ? ? }


? ? ? ? ? ? return IPAddress.None.ToString(); //255.255.255.255?

? ? ? ? }


? ? ? ? #endregion


? ? }


總結(jié)

以上是生活随笔為你收集整理的C#获取电脑IP、MAC地址示例代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。