当前位置:网站首页>C# 32. 静态类实现下拉框选择网卡
C# 32. 静态类实现下拉框选择网卡
2022-06-09 18:16:00 【lljss2020】
//NetIf.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace Xxxxx
{
class NetIf
{
public struct NetIfType
{
public string netIf;
public string Ip;
}
public static List<NetIfType> NetIfList = new List<NetIfType>();
public static int selectNetIf = 0;
public static List<NetIfType> GetNetIf()
{
List<NetIfType> listStr = new List<NetIfType>();
//获取所有网卡信息
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
//判断是否为以太网卡
//Wireless80211 无线网卡 Ppp 宽带连接
//Ethernet 以太网卡
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
//获取以太网卡网络接口信息
IPInterfaceProperties ip = adapter.GetIPProperties();
//获取单播地址集
UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
foreach (UnicastIPAddressInformation ipadd in ipCollection)
{
//InterNetwork IPV4地址 InterNetworkV6 IPV6地址
//Max MAX 位址
if (ipadd.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
listStr.Add(new NetIfType()
{
netIf = adapter.Description,
Ip = ipadd.Address.ToString()
});
}
}
}
}
return listStr;
}
//选择网卡上的本地IP
public static string GetLocalIp(int index)
{
if (index>= 0)
{
return NetIfList[index].Ip;
}
else
{
return null;
}
}
}
}
private void comboBoxNetIf_DropDown(object sender, EventArgs e)
{
NetIf.NetIfList.Clear();
NetIf.NetIfList = NetIf.GetNetIf();
comboBoxNetIf.Items.Clear();
foreach (NetIf.NetIfType m in NetIf.NetIfList)
{
comboBoxNetIf.Items.Add(m.netIf);
}
}
//在非UI线程中
private void UserThread()
{
string localIp = null;
int flag;
Invoke((EventHandler)(delegate
{
NetIf.selectNetIf = comboBoxNetIf.SelectedIndex;
}));
localIp = NetIf.GetLocalIp(NetIf.selectNetIf);
.......
}
实际效果:
边栏推荐
- [MySQL string data type optimization] the difference between char and varchar
- 君可归烈士寻亲系统开发实战
- php使用unlink删除文件提示无权限
- MySQL community server 8.0.29 installation and configuration method graphic tutorial
- 梦醒
- Pytorch set random seed
- In depth learning and CV tutorial (10) | lightweight CNN architecture (squeezenet, shufflenet, mobilenet, etc.)
- 如何实现自定义富文本编辑器标签
- Golang基础(4)
- CORTEX-A9三星iTOP-4412开发开发板入门嵌入式
猜你喜欢

Process control -- > > process termination

CNN - nn. Conv1d use

AI chief architect 4-aica-baidu CV technology application and industry landing experience

AI首席架构师3-AICA-智慧城市中的AI应用实践

redis源码学习-03_动态字符串SDS

JsonPath-教程

充电桩的B面是......不只公众号?还有智充小程序!

Singular Value Decomposition(SVD)

直播预告 | 在阿里云 ESSD 云盘上部署 PolarDB for PostgreSQL 集群

君可归烈士寻亲系统开发实战
随机推荐
EasyCVR级联EasyCVR出现通道频繁掉线是什么原因?
深入理解联合索引的最左前缀原则
电脑小知识与优化
NLP keyword extraction overview
CNN - nn. Conv1d use
Redis source code learning-04_ Linked list
Singular Value Decomposition(SVD)
kubectl 最新常用命令 --V1.24版本
Determination principle of abbexa DUT ELISA Kit
Analysis of C language [advanced] paid knowledge [II]
ZigBee组网从未如此简单!
Redis basic and advanced
How to learn the process of KD tree construction and search with cases?
【玩转华为云】MapReduce服务初体验
10个常见触发IO瓶颈的高频业务场景
crontab定时执行任务
提升Typecho首页ttfb加载速度以及若干升级
华为云原生之数据仓库服务GaussDB(DWS)的深度使用与应用实践【这次高斯不是数学家】
转:彼得·德鲁克:什么样的老师才是真正的老师?
Redis知识点&面试题总结