发现好长时间没写技术方面的东西了,不免有些生疏了,这几天工作状态不错,随便写点东西吧。
好多用notebook的朋友比较苦恼的一个问题,在公司和在家里的网络设置不一样,于是上班和回到家的时候都要更改网卡的网络设置。
我也是比较烦这个东西,每天改来改去的很麻烦,今天心血来潮,决定写一个小程序简化一下这个步骤。
查了一下dotnet相关方面的东西和文章,发现的确可行。
using System.Management 这个里面封装了这些东东,
网卡的设置无非就是四项,IP地址,子网掩码,网关,DNS
设置四个string型变量:
private string ipaddress;
private string subcode;
private string netgate;
private string dns;
private string IpAddree
{
set
{
ipaddress=value;
}
get
{
return ipaddress;
}
}
private string SubCode
{
set
{
subcode=value;
}
get
{
return subcode;
}
}
private string NetGate
{
set
{
netgate=value;
}
get
{
return netgate;
}
}
private string Dns
{
set
{
dns=value;
}
get
{
return dns;
}
}
利用下面这个方法来更改网卡设置:
private void changeip()
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
//遍历所有在工作状态的网卡
foreach(ManagementObject mo in moc)
{
if(! (bool) mo["IPEnabled"])
continue;
//Set IPAddress & SubnetMask
inPar = mo.GetMethodParameters("EnableStatic");
inPar["IPAddress"] = new string[] { IpAddree };
inPar["SubnetMask"] = new string[] { SubCode };
outPar = mo.InvokeMethod("EnableStatic", inPar, null);
//set Getway
inPar = mo.GetMethodParameters("SetGateways");
inPar["DefaultIPGateway"] = new string[]{ NetGate };
outPar = mo.InvokeMethod("SetGateways", inPar, null);
//set dns
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = new string[] { Dns};
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
}
}
就是这么简单,根据以上的办法去msdn的库里面查下相关方面的东西,网卡的大多数属性都是可以通过.net程序来更改的。
下面是程序的全部代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;
namespace IpAdd
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.ComboBox cbbAddress;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private string ipaddress;
private string subcode;
private string netgate;
private string dns;
private string IpAddree
{
set
{
ipaddress=value;
}
get
{
return ipaddress;
}
}
private string SubCode
{
set
{
subcode=value;
}
get
{
return subcode;
}
}
private string NetGate
{
set
{
netgate=value;
}
get
{
return netgate;
}
}
private string Dns
{
set
{
dns=value;
}
get
{
return dns;
}
}
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.cbbAddress = new System.Windows.Forms.ComboBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cbbAddress
//
this.cbbAddress.Items.AddRange(new object[] {
"192.168.6.14",
"192.168.1.9",
"192.168.0.9"});
this.cbbAddress.Location = new System.Drawing.Point(72, 16);
this.cbbAddress.Name = "cbbAddress";
this.cbbAddress.Size = new System.Drawing.Size(144, 20);
this.cbbAddress.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(104, 64);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "更 改";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(288, 141);
this.Controls.Add(this.button1);
this.Controls.Add(this.cbbAddress);
this.Name = "Form1";
this.Text = "更改IP地址";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
if(cbbAddress.Text.ToString()!="")
{
this.IpAddree = cbbAddress.Text.ToString();
if(IpAddree=="192.168.1.9")
{
this.SubCode = "255.255.255.0";
this.NetGate = "192.168.1.1";
this.Dns = "202.106.0.20";
}
else if(IpAddree=="192.168.6.14")
{
this.SubCode = "255.255.255.0";
this.NetGate = "192.168.6.250";
this.Dns = "192.168.0.10";
}
}
changeip();
MessageBox.Show("ip地址更改完毕");
}
private void changeip()
{
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if(! (bool) mo["IPEnabled"])
continue;
//Set IPAddress & SubnetMask
inPar = mo.GetMethodParameters("EnableStatic");
inPar["IPAddress"] = new string[] { IpAddree };
inPar["SubnetMask"] = new string[] { SubCode };
outPar = mo.InvokeMethod("EnableStatic", inPar, null);
//set Getway
inPar = mo.GetMethodParameters("SetGateways");
inPar["DefaultIPGateway"] = new string[]{ NetGate };
outPar = mo.InvokeMethod("SetGateways", inPar, null);
//set dns
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = new string[] { Dns};
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
}
}
}
}
嘿嘿 现在再也不用手工更改网络设置那么麻烦了。
其实这个东西完全可以写一个config文件,功能变得更强大一点。不过最近时间比较紧,写这些东西仅仅有一个小时不到的时间
,周末就要回大连了。等考完试有时间再看看吧。