|

分享源码
界面截图: |
|
是否带模块: |
纯源码 |
备注说明: |
- |
最近在搞nei网穿透,用到cha询网卡信息并修改,翻了论坛,下载的一些都有些问题,比如兼容性不好,有的在win10是好的,我拿到win7或者windows server2008,2019上去,网卡就枚举有问题了,哎,所以就有了这个dll,c#弄 的,兼容win7~11,server等平台。[C#] 纯文本查看 复制代码 using System;
using System.Net.NetworkInformation;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Linq;
using System.Management;
using RGiesecke.DllExport;
namespace NetworkInfoDll
{
public class NetworkInfo
{
private static List<NetworkInterface> networkInterfaces;
[DllExport("GetNetworkAdaptersCount", CallingConvention = CallingConvention.StdCall)]
public static int GetNetworkAdaptersCount()
{
networkInterfaces = new List<NetworkInterface>(NetworkInterface.GetAllNetworkInterfaces());
return networkInterfaces.Count;
}
[DllExport("GetNetworkAdapterInfo", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static string GetNetworkAdapterInfo(int index)
{
if (networkInterfaces == null || index < 0 || index >= networkInterfaces.Count)
{
return "Error: Invalid index";
}
NetworkInterface nic = networkInterfaces[index];
return GetNetworkAdapterInfoByInterface(nic);
}
[DllExport("GetActiveNetworkAdapterInfo", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static string GetActiveNetworkAdapterInfo()
{
NetworkInterface activeNic = GetActiveNetworkInterface();
if (activeNic == null)
{
return "Error: No active network adapter found";
}
return GetNetworkAdapterInfoByInterface(activeNic);
}
[DllExport("SetNetworkAdapterInfo", CallingConvention = CallingConvention.StdCall)]
[return: MarshalAs(UnmanagedType.LPStr)]
public static string SetNetworkAdapterInfo(string connectionName, string ipAddress, string subnetMask, string gateway)
{
try
{
if (!IsAdministrator())
{
return "Error: 需要管理员权限来修改网络设置";
}
using (var networkConfigMng = new ManagementClass("Win32_NetworkAdapter"))
{
using (var networkConfigs = networkConfigMng.GetInstances())
{
foreach (ManagementObject networkAdapter in networkConfigs)
{
string netConnectionID = networkAdapter["NetConnectionID"] as string;
if (netConnectionID == connectionName)
{
uint index = (uint)networkAdapter["Index"];
using (var networkConfigMng2 = new ManagementClass("Win32_NetworkAdapterConfiguration"))
{
using (var networkConfigs2 = networkConfigMng2.GetInstances())
{
foreach (ManagementObject networkConfig in networkConfigs2)
{
if ((uint)networkConfig["Index"] == index)
{
ManagementBaseObject newIP = networkConfig.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = new string[] { ipAddress };
newIP["SubnetMask"] = new string[] { subnetMask };
ManagementBaseObject newGateway = networkConfig.GetMethodParameters("SetGateways");
newGateway["DefaultIPGateway"] = new string[] { gateway };
ManagementBaseObject setIP = networkConfig.InvokeMethod("EnableStatic", newIP, null);
ManagementBaseObject setGateway = networkConfig.InvokeMethod("SetGateways", newGateway, null);
if ((uint)setIP["ReturnValue"] == 0 && (uint)setGateway["ReturnValue"] == 0)
{
return "Success: 网络设置已更新";
}
else
{
return $"Error: 设置IP返回值 {setIP["ReturnValue"]}, 设置网关返回值 {setGateway["ReturnValue"]}";
}
}
}
}
}
}
}
}
}
return $"Error: 未找到名为 '{connectionName}' 的网络连接";
}
catch (Exception ex)
{
return $"Error: {ex.Message}";
}
}
private static NetworkInterface GetActiveNetworkInterface()
{
return NetworkInterface.GetAllNetworkInterfaces()
.OrderByDescending(nic => nic.Speed)
.FirstOrDefault(nic =>
nic.OperationalStatus == OperationalStatus.Up &&
nic.NetworkInterfaceType != NetworkInterfaceType.Loopback &&
nic.GetIPProperties().UnicastAddresses.Any(addr => addr.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork));
}
private static string GetNetworkAdapterInfoByInterface(NetworkInterface nic)
{
IPInterfaceProperties properties = nic.GetIPProperties();
string connectionName = nic.Name;
string adapterName = nic.Description;
string status = GetChineseStatus(nic.OperationalStatus);
string ipAddress = "";
string subnetMask = "";
string gateway = "";
foreach (UnicastIPAddressInformation ip in properties.UnicastAddresses)
{
if (ip.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
ipAddress = ip.Address.ToString();
subnetMask = ip.IPv4Mask.ToString();
break;
}
}
if (properties.GatewayAddresses.Count > 0)
{
gateway = properties.GatewayAddresses[0].Address.ToString();
}
return $"{connectionName}|{adapterName}|{status}|{ipAddress}|{subnetMask}|{gateway}";
}
private static string GetChineseStatus(OperationalStatus status)
{
switch (status)
{
case OperationalStatus.Up:
return "已连接";
case OperationalStatus.Down:
return "已断开";
case OperationalStatus.Testing:
return "正在测试";
case OperationalStatus.Unknown:
return "未知";
case OperationalStatus.Dormant:
return "休眠";
case OperationalStatus.NotPresent:
return "不存在";
case OperationalStatus.LowerLayerDown:
return "底层已断开";
default:
return status.ToString();
}
}
private static bool IsAdministrator()
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
return principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
}
}
}
窗口程序集名 | 保 留 | 保 留 | 备 注 | 窗口程序集_启动窗口 | | | |
a = 获取网卡数量 () 计次循环首 (a, i ) 调试输出 (获取网卡信息 (i - 1 ))  i = i + 1 计次循环尾 ()调试输出 (获取当前联网网卡信息 ()) 调试输出 (设置网卡 (“WLAN”, “10.36.0.199”, “255.255.255.0”, “10.36.0.1”))
|
评分
-
查看全部评分
|