设置网络配置

作者:追风剑情 发布于:2023-2-23 16:47 分类:C#

转载于: https://blog.csdn.net/weixin_30622181/article/details/98771710

ManagementClass类
ManagementBaseObject类
ManagementObject类
ManagementObjectSearcher类

using System;
using System.Collections.Generic;
using System.Management; //需要添加引用System.Management.dll

public sealed class NetworkUtil
{
	//WMI命名空间
	private const string WMI_NAMESPACE = "Win32_NetworkAdapterConfiguration";
	//WMI类
	private const string WMI_CLASS = "root\\CIMV2";

	// 查询网卡信息
	public static List<NetworkAdapterInfo> GetNetworkAdapters()
	{
		List<NetworkAdapterInfo> adapters = new List<NetworkAdapterInfo>();
		// 指定查询网卡信息 ( IPEnabled 为 True 的 )
		string strQry = "Select * from Win32_NetworkAdapterConfiguration where IPEnabled=True";
		// ManagementObjectSearcher 类 ,根据指定的查询获取物理控件的集合。
		ManagementObjectSearcher objSc = new ManagementObjectSearcher(strQry);
		// 用foreach获取元素
		foreach (ManagementObject objQry in objSc.Get())
		{
			uint index = (uint)objQry["Index"];//网卡序号
			string desc = (string)objQry["Description"];//网卡描述
			NetworkAdapterInfo adapter = new NetworkAdapterInfo();
			adapter.index = index;
			adapter.description = desc;
			adapters.Add(adapter);
			//Console.WriteLine("网卡: Index={0}, Description={1}", index, desc);
		}
		return adapters;
	}

	// 设置网络配置
	public static void SetNetConfig(string strIndex, string strIP, string strSubmask, 
		string strGateway, string strDNS1, string strDNS2)
	{
		ManagementObject objCls = new ManagementObject(WMI_CLASS, WMI_NAMESPACE+".INDEX="+ strIndex, null);
		ManagementBaseObject objInPara;
		objInPara = objCls.GetMethodParameters("EnableStatic");
		objInPara["IPAddress"] = new string[] { strIP }; // 设置IP
		objInPara["SubnetMask"] = new string[] { strSubmask }; // 设置子网掩码
		objCls.InvokeMethod("EnableStatic", objInPara, null);
		objInPara = objCls.GetMethodParameters("SetGateways");
		objInPara["DefaultIPGateway"] = new string[] { strGateway }; // 设置网关
		objCls.InvokeMethod("SetGateways", objInPara, null);
		//设置DNS
		//objInPara = objCls.GetMethodParameters("SetDNSServerSearchOrder");
		//objInPara["DNSServerSearchOrder"] = new string[] { strDNS1, strDNS2 };
		//objCls.InvokeMethod("SetDNSServerSearchOrder", objInPara, null);
		// GetMethodParameters 方法 : 用來取得 SetDNSServerSearchOrder 的参数列表。
		// InvokeMethod 方法 : 调用。
	}

	// 设置自动获取网络配置
	public static void AutoNetConfig(string strIndex)
	{
		// 建立 ManagementObject 对象 ( Scope , Path , options )
		ManagementObject objCls = new ManagementObject(WMI_CLASS, WMI_NAMESPACE + ".INDEX=" + strIndex, null);
		// InvokeMethod 方法 : 在对象上调用方法。
		objCls.InvokeMethod("EnableDHCP", null); // 设置自动获取 IP
		objCls.InvokeMethod("ReleaseDHCPLease", null); // 释放 IP
		objCls.InvokeMethod("RenewDHCPLease", null); // 重新取得 IP
	}

	// 仅测试用
	public static void TestNetConfig()
	{
		string strIndex = "5"; //网卡序号
		string strIP = "192.168.1.11"; // IP地址
		string strSubmask = "255.255.255.0"; // 子网掩码
		string strGateway = "192.168.1.1"; // 网关
		string strDNS1 = "61.139.2.69"; // 首选DNS
		string strDNS2 = "168.95.192.1"; // 备用DNS
		//设置网卡信息
		SetNetConfig(strIndex, strIP, strSubmask, strGateway, strDNS1, strDNS2);
	}

	// 仅测试用
	public static void TestAutoNetConfig()
	{
		string strIndex = "5"; //网卡序号
		AutoNetConfig(strIndex);
	}
}

public class NetworkAdapterInfo
{
	public uint index;
	public string description;

	public override string ToString()
	{
		return string.Format("网卡: Index={0}, {1}", index, description);
	}
}

标签: C#

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号