2024年1月15日发(作者:)

VS2010实现IP自动获取和配置

本人的工作和住所上网的方式不同,工作场所是固定IP,绑定MAC地址和IP地址才能上网;而在住所则是自动获取IP地址即可上网。由于本人较为懒散,每次使用Windows提供的IP设置来设置IP地址,子网掩码,网关,DNS服务器的时候,总觉得很麻烦。本人大学的时候学的是计算机专业,终于发现有用武之地了。通过查阅资料,本人使用VS2010,开发了一个简单的IP配置器。该IP配置器实现了IP的固定设置和自动获取IP地址,同时能记录用户的多个IP配置,方便不止两个地方上网的用户使用。现将开发经验拿出来晒一晒。

在Windows中,WMI(Windows Management Instrumentation,Windows 管理规范)是一项核心的 Windows 管理技术;用户可以使用 WMI 管理本地和远程计算机。WMI可以帮助用户配置和管理IP设置。本程序中使用了该管理技术。

开发主要步骤如下:

1、在VS2010中创建项目,项目设置为基于对话框的,使用C#编写。

2、为项目添加引用,如下图所示

3、设计窗体

4、XML文件的组织

在程序中,需要保存用户的IP配置。XML是一种公用的数据保存方式,因此在程序中采用XML文件来保存用户配置。文件组织如下所示:

192.168.1.3

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.3

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.3

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.3

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.3

192.168.1.2

192.168.1.2

192.168.1.2

192.168.1.2

5、主要源码

(1)自动获取IP地址

///

/// 自Á?动¡¥获?取¨?IP地Ì?址¡¤

///

///

///

private void

button_AutoSetIP_Click(object sender,

EventArgs e)

{

try

{

EnableDHCP();

(this,"DHCP Setting

Success!", "Success", ,

ation);

}

catch (Exception error)

{

(this,

e, "DHCP Error",

,

);

//throw;

}

}

///

/// 启?用®?DHCP服¤t务?器¡Â

///

public static void EnableDHCP()

{

ManagementClass wmi = new

ManagementClass("Win32_NetworkAdapterConfiguration");

ManagementObjectCollection

moc = tances();

foreach (ManagementObject mo

in moc)

{

if

(!(bool)mo["IPEnabled"])

continue;

if ((bool)mo["IPEnabled"]

== true)

{

//(mo["Caption"].ToString());

//重?置?DNS为a空?

Method("SetDNSServerSearchOrder",

null);

//开a启?DHCP

Method("EnableDHCP", null);

}

} }

(2)检查IP地址是否合法,使用正则表达式

public static bool CheckIPAddress(string ip)

{

//IP地Ì?址¡¤的Ì?正y则¨°表À¨ª达ä?式º?

Regex rx = new

Regex(@"((?:(?:25[0-5]|2[0-4]d|((1d{2})|([1-9]?d))).){3}(?:25[0-5]|2[0-4]d|((1d{2})|([1-9]?d))))");

return h(ip);

}

(3)设置固定IP地址

///

/// 修T改?IP地Ì?址¡¤

///

/// ip地Ì?址¡¤

/// 子Á¨®网ª?掩¨²码?

/// 默?认¨?网ª?关?

/// DNS地Ì?址¡¤

public static void SetIPAddress(ManagementObject mo, string[] ipAddress, string []

subnetMask, string [] defaultGetway, string [] DNS1, string [] DNS2)

{

ManagementBaseObject inPar = null;

ManagementBaseObject outPar = null;

//设¦¨¨置?IP地Ì?址¡¤和¨ª掩¨²码?

if (!(ipAddress == null) && !(subnetMask == null))

{

inPar = hodParameters("EnableStatic");

inPar["IPAddress"] = ipAddress;

inPar["SubnetMask"] = subnetMask;

outPar = Method("EnableStatic", inPar, null);

}

//设¦¨¨置?网ª?关?地Ì?址¡¤

if (!(defaultGetway == null))

{

inPar = hodParameters("SetGateways");

inPar["DefaultIPGateway"] = defaultGetway;

outPar = Method("SetGateways", inPar, null);

}

//设¦¨¨置?DNS地Ì?址¡¤

if (!(DNS1 == null) && !(DNS2 == null))

{

string [] dns = new string[ + ];

dns = (DNS2).ToArray();

inPar = hodParameters("SetDNSServerSearchOrder");

inPar["DNSServerSearchOrder"] = dns;

outPar = Method("SetDNSServerSearchOrder", inPar, null);

}

}

(4)获取网络适配器

///

/// 获?取¨?所¨´有®D网ª?络?接¨®口¨²对?象¨®参?数ºy

///

///

///

public int GetAllAdapterInfo(DataTable table)

{

NetworkInterface[] adapters = NetworkInterfaces();//获?取¨?本À?地Ì?计?算?机¨²上¦?网ª?络?接¨®口¨²的Ì?对?象¨®

("Desc", e(""));

("ID", e(""));

("Name", e(""));

("Type", e(""));

("Speed", e(""));

("Status", e(""));

("MACAddress", e(""));

foreach (NetworkInterface item in adapters)

{

DataRow dr = ();

dr[0] = ng();

dr[1] = ng();

dr[2] = ng();

if (ng() == "本À?地Ì?连¢?接¨®")

{

//("aaa");

this.m_LocalEthernetAdapterName = ng();

}

dr[3] = ng();

dr[4] = * 0.001 * 0.001;

dr[5] = ng();

PhysicalAddress pa = sicalAddress();

byte[] bytes = ressBytes();//返¤¦Ì回?当Ì¡À前¡ã实º¦Ì例¤y的Ì?地Ì?址¡¤

StringBuilder sb = new StringBuilder();

for (int i = 0; i < ; i++)

{

(bytes[i].ToString("X2"));//以°?十º?六¢¨´进?制?格?式º?化¡¥

if (i != - 1)

{

("-");

}

}

dr[6] = sb;

(dr);

}

return ;

}

程序运行结果截图: