asp.net,java 之修改IP等信息,读取IP配置信息后台代码
来源:互联网 发布:诸暨行知小学刘海民 编辑:程序博客网 时间:2024/05/14 15:25
当初写这个的时候在网上查了多好资料,终于稀里糊涂的写出来了,放在这里,以后忘的时候看看
注:我这个是一个多网卡的代码,和单网卡一样的,就是多一层判断
//自动获取IP nicname为网卡信息,也就是网卡ID,这样才能确定要为哪个网卡配置
public static void GetIp(string nicname)
{
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = wmi.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["IPEnabled"])
{
if (mo["Caption"].Equals(nicname))
{
//重置DNS为空
mo.InvokeMethod("SetDNSServerSearchOrder", null);
//开启DHCP
mo.InvokeMethod("EnableDHCP", null);
break;
}
}
}
}
//设置IP地址,掩码,网关和DNS,这些参数应该不难看懂吧,注意传过的都是数组的形式
public static void SetIPAddress(string nicname,string[] ip, string[] submask, string[] getway, string[] dns)
{
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = wmi.GetInstances();
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
foreach (ManagementObject mo in moc)
{
//如果没有启用IP设置的网络设备则继续,主要是多网卡不可能每个网卡本地连接都连接着,这样就算是本地连接被断开也可以配置的
if (!(bool)mo["IPEnabled"])
continue;
if (mo["Caption"].Equals(nicname))
{
//设置IP地址和掩码
if (ip != null && submask != null)
{
inPar = mo.GetMethodParameters("EnableStatic");
inPar["IPAddress"] = ip;
inPar["SubnetMask"] = submask;
outPar = mo.InvokeMethod("EnableStatic", inPar, null);
}
//设置网关地址
if (getway != null)
{
inPar = mo.GetMethodParameters("SetGateways");
inPar["DefaultIPGateway"] = getway;
outPar = mo.InvokeMethod("SetGateways", inPar, null);
}
//设置DNS地址
if (dns != null)
{
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = dns;
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
}
}
}
}
//获取网络适配器信息,以LIST形式返回,去遍历就可以出来了
public static ArrayList GetNICNames()
{
ArrayList nicNames = new ArrayList();
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;
nicNames.Add(mo["Caption"]);
}
return nicNames;
}
//获取网卡IP信息等,注意,这里使用了out string这种类型,不懂得去查,因为我也查出来的,嘿嘿
public static void getMessage(string nicname, out string[] ipAdresses, out string[] subnets, out string[] gateways, out string[] dnses)
{
ipAdresses = null;
subnets = null;
gateways = null;
dnses = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"])
continue;
if (mo["Caption"].Equals(nicname))
{
ipAdresses = (string[])mo["IPAddress"];
subnets = (string[])mo["IPSubnet"];
gateways = (string[])mo["DefaultIPGateway"];
dnses = (string[])mo["DNSServerSearchOrder"];
break;
}
}
}
后台代理基本上就这些了,应该没差下什么吧。。。。。。
- asp.net,java 之修改IP等信息,读取IP配置信息后台代码
- asp.net 获取 IP信息
- ASP.NET(C#)中获取IP,名称,操作系统,浏览器等信息代码大全
- ASP.NET获取客户端IP/用户名等信息
- ASP.NET获取客户端IP/用户名等信息
- ASP.NET获取客户端IP/用户名等信息
- ASP.NET获取客户端IP/用户名等信息
- ASP.NET获取客户端IP/用户名等信息
- ASP.NET获取客户端IP/用户名等信息
- asp.net获取服务器IP等相关信息
- ASP.NET获取客户端IP/用户名等信息
- bsd修改IP、子网掩码、路由等信息
- ubuntu指令模式修改IP等信息
- Linux -- 修改IP信息
- 修改网卡IP信息
- ASP.NET中取得当前服务器名、IP及客户端等信息
- ASP.NET获取IP及电脑名等信息的简单方法+通用类文件源码
- ASP.NET(C#)中如何获取IP,名称,操作系统,浏览器等信息
- eclipse快捷键大全
- ogre中的鼠标拾取
- Messagebox的Owner,显示位置,自动关闭的研究。
- NAND坏块
- 调试Bug的神兵利器:通过WinDbg条件断点收集Log
- asp.net,java 之修改IP等信息,读取IP配置信息后台代码
- Symbian函数或类与头文件和LIB库对照表
- Android 中的长度单位详解 dp、sp、px、in、pt、mm 转载
- Java与模式:缺省适配器模式【转自www.bitsCN.com】
- 关于MySQL的LIMIT 语法小优化!
- 经典小函数实现汇总(陆续完善)
- Creating an Application by Using Windows Presentation Foundation
- Five Great WPF 3D Nuggets
- cin、cin.get()、cin.getline()、getline()、gets()等函数的用法