bingil C#利用注册表获取及修改不同网上的IP地址 lanix

借了个笔记本,在不同地方经常要更改IP地址,索性写个程序。在网上搜索了很多代码, ManagementBaseObject的用法无效。最后感觉既然所有的TcpIP和DNS值在注册表中都有,那是不是可以。。

看效果图

下面是具体代码:

保存IP数据的:IpGroup.cs

  1     public class IpGroup
2 {
3 #region get/set
4 private string ipAddress;
5
6 public string IpAddress
7 {
8 get { return ipAddress; }
9 set { ipAddress = value; }
10 }
11 private string ipSubnetMask;
12
13 public string IpSubnetMask
14 {
15 get { return ipSubnetMask; }
16 set { ipSubnetMask = value; }
17 }
18
19 private string ipGateway;
20
21 public string IpGateway
22 {
23 get { return ipGateway; }
24 set { ipGateway = value; }
25 }
26
27 private string firstDns;
28
29 public string FirstDns
30 {
31 get { return firstDns; }
32 set { firstDns = value; }
33 }
34
35 private string secondDns;
36
37 public string SecondDns
38 {
39 get { return secondDns; }
40 set { secondDns = value; }
41 }
42
43 private string netWorkcard;
44
45 public string NetWorkcard
46 {
47 get { return netWorkcard; }
48 set { netWorkcard = value; }
49 }
50
51 #endregion
52
53
54
55 public IpGroup(string netWorkcard)
56 {
57 this.netWorkcard = netWorkcard;
58 ipAddress = "";
59 ipSubnetMask = "";
60 ipGateway = "";
61 firstDns = "";
62 secondDns = "";
63 }
64
65 public void getTcpIp()
66 {
67 IpChangerByRegistry ipc = new IpChangerByRegistry();
68 IpGroup ipg = ipc.getTcpIp(NetWorkcard);
69 this.ipAddress = ipg.IpAddress;
70 this.ipSubnetMask = ipg.IpSubnetMask;
71 this.ipGateway = ipg.IpGateway;
72 this.firstDns = ipg.FirstDns;
73 this.secondDns = ipg.SecondDns;
74 }
75 public void setTcpIp()
76 {
77 IpChangerByRegistry ipc = new IpChangerByRegistry();
78 ipc.setTcpIp(this.netWorkcard, this);
79 }
80
81 public string toString()
82 {
83 string res = "";
84 res = netWorkcard + ":\nIPAddress: " + ipAddress + ";\nSubnetMask: " + ipSubnetMask +
85 ";\nDefaultGateway: " + ipGateway + ";\nFirstDNS:" + firstDns +
86 ";\nSecondDNS: " + secondDns + ".\n";
87 return res;
88 }
89
90 public int settingCount()
91 {
92 int count = 0;
93 if (!String.IsNullOrWhiteSpace(ipAddress))
94 count++;
95 if (!String.IsNullOrWhiteSpace(ipSubnetMask))
96 count++;
97 if (!String.IsNullOrWhiteSpace(ipGateway))
98 count++;
99 if (!String.IsNullOrWhiteSpace(firstDns))
100 count++;
101 if (!String.IsNullOrWhiteSpace(secondDns))
102 count++;
103 return count;
104 }
105 }

操作注册表的:RegistryOperator.cs

    class RegistryOperator
{
private RegistryKey keyRoot;

public RegistryOperator(RegistryKey keyRoot)
{
this.keyRoot = keyRoot;

}
public void close()
{
keyRoot.Close();
}

#region get/delete/add RegistryKey

public RegistryKey getRegistryKey(string keyPath)
{
return keyRoot.OpenSubKey(keyPath);
}

public bool deleteRegistryKey(string keyPath)
{
try
{
keyRoot.DeleteSubKey(keyPath, true);
return true;
}
catch (Exception)
{
return false;
}

}
public bool addRegistryKey(string keyPath)
{
try
{
keyRoot.CreateSubKey(keyPath);
return true;
}
catch (Exception)
{
return false;
}
}
#endregion

#region get/delete/add/alter the value of RegistryKey

public string getRegistryValue(string keyPath, string name)
{
try
{
RegistryKey key = keyRoot.OpenSubKey(keyPath, true);
string res = key.GetValue(name).ToString();
if (!res.Equals("System.String[]"))
return res;
string[] r = (string[])key.GetValue(name);
return r[0];
}
catch (Exception)
{
return "";
}
}
public bool deleteRegistryValue(string keyPath, string name)
{
try
{
RegistryKey key = keyRoot.OpenSubKey(keyPath, true);
key.DeleteValue(name);
key.Close();
return true;
}
catch (Exception)
{
return false;
}
}
public bool addRegistryValue(string keyPath, string name, string value)
{
try
{
RegistryKey key = keyRoot.OpenSubKey(keyPath, true);
key.SetValue(name, value);
key.Close();
return true;
}
catch (Exception)
{
return false;
}
}
public bool alterRegistryValue(string keyPath, string name, string newValue)
{
try
{
RegistryKey key = keyRoot.OpenSubKey(keyPath, true);
key.DeleteValue(name);
key.SetValue(name, newValue);
key.Close();
return true;
}
catch (Exception)
{
return false;
}
}
#endregion
}

修改IP的接口:IIpChanger

public interface IIpChanger
{
IpGroup getTcpIp(string networkCardName);
void setTcpIp(string networkCardName, IpGroup ipg);
}

利用注册表修改IP:IpChangerByRegistry.cs

class IpChangerByRegistry:IIpChanger
{
private const string PATH_TCP_SERVICES = @"SYSTEM\CurrentControlSet\Services";
private const string PATH_TCP_SUFFIX = @"Parameters\Tcpip";
private const string PATH_NETWORDCARD = @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkCards";
private const string PATH_TCPIP_ADAPTERS = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Adapters";
private const string PATH_TCPIP_INTERFACES = @"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces";
private string[] tcpNameOfInterfaces = new string[] { "IPAddress", "SubnetMask", "DefaultGateway", "NameServer" };

RegistryOperator regOperator;
private RegistryKey keyNetWorkCard;
private RegistryKey keyInterfaces;

private Dictionary<string, string> netWorkCards;

public Dictionary<string, string> NetWorkCards
{
get { return netWorkCards; }
set { netWorkCards = value; }
}

public IpChangerByRegistry()
{
regOperator = new RegistryOperator(Registry.LocalMachine);
keyNetWorkCard = regOperator.getRegistryKey(PATH_NETWORDCARD);
keyInterfaces = regOperator.getRegistryKey(PATH_TCPIP_INTERFACES);

netWorkCards = new Dictionary<string, string>();
initNetWorkcards();
}

private void initNetWorkcards()
{
string[] cards = keyNetWorkCard.GetSubKeyNames();
foreach (string c in cards)
{
string path = PATH_NETWORDCARD + "\\" + c;
string dkey = regOperator.getRegistryValue(path, "Description");
string dval = regOperator.getRegistryValue(path, "ServiceName");
netWorkCards.Add(dkey, dval);
}
}

#region 获取/设置TcpIp

public IpGroup getTcpIp(string networkCardName)
{
IpGroup ipg = new IpGroup(networkCardName);
if (netWorkCards.ContainsKey(networkCardName))
networkCardName = netWorkCards[networkCardName];
if (!netWorkCards.ContainsValue(networkCardName))
return null;

string path = PATH_TCPIP_INTERFACES + "\\" + networkCardName;
ipg.IpAddress = regOperator.getRegistryValue(path, tcpNameOfInterfaces[0]);
ipg.IpSubnetMask = regOperator.getRegistryValue(path, tcpNameOfInterfaces[1]);
ipg.IpGateway = regOperator.getRegistryValue(path, tcpNameOfInterfaces[2]);
string dns = regOperator.getRegistryValue(path, tcpNameOfInterfaces[3]);
string[] dnses = dns.Split(',');
if (dnses.Length > 0)
ipg.FirstDns = dnses[0].Trim();
if (dnses.Length > 1)
ipg.SecondDns = dnses[1].Trim();

return ipg;

}
public void setTcpIp(string networkCardName, IpGroup ipg)
{
if (netWorkCards.ContainsKey(networkCardName))
networkCardName = netWorkCards[networkCardName];
if (!netWorkCards.ContainsValue(networkCardName))
return;

string path = PATH_TCPIP_INTERFACES + "\\" + networkCardName;
regOperator.alterRegistryValue(path, tcpNameOfInterfaces[0], ipg.IpAddress);
regOperator.alterRegistryValue(path, tcpNameOfInterfaces[1], ipg.IpSubnetMask);
regOperator.alterRegistryValue(path, tcpNameOfInterfaces[2], ipg.IpGateway);
string dns = ipg.FirstDns + "," + ipg.SecondDns;
regOperator.alterRegistryValue(path, tcpNameOfInterfaces[3], dns);
}

#endregion
}

操作IPXML文档的:IPXmlHandler.cs

class IPXmlHandler
{
private string[] strName = { "IP", "Subnet", "Gateway", "DNS", "CopyDNS" };
private string fileName;
private XmlDocument xmld;
private Hashtable hst;

public IPXmlHandler(string fileName)
{
this.fileName = fileName;
xmld = new XmlDocument();
xmld = new XmlDocument();

hst = new Hashtable();
getIPXmlDataToHst();
}

public Hashtable Hst
{
get { return hst; }
set { hst = value; }
}

/// <summary>
/// 同步xml文档与hst
/// </summary>
public bool synbiosis(string elemName, string[] data)
{
if (hst.ContainsKey(elemName))
return false;
else
{
hst.Add(elemName, data);
XmlNode xmln = getIPXmlNode(elemName, data);
addIPXmlNode(xmln);
return true;
}
}

public bool synbiosis(string userName)
{
if(!hst.ContainsKey(userName))
return false;
else
{
hst.Remove(userName);
delIPXmlNode(userName);
return true;
}
}

/// <summary>
/// 从xml中获取用户名及其IP设置,存入哈希表
/// </summary>
private void getIPXmlDataToHst()
{
xmld.Load(fileName);
XmlNode xmlRoot = xmld.SelectSingleNode("AddressList");
XmlNodeList xmlnlist = xmlRoot.ChildNodes;

foreach (XmlNode xn in xmlnlist)
{
string strName = "";
string[] data = new string[5];
strName = xn.Name;
XmlNodeList xl = xn.ChildNodes;
int i = 0;
foreach (XmlNode xn2 in xl)
{
data[i] = xn2.InnerText;
i++;
}
if(!hst.ContainsKey(strName))
hst.Add(strName, data);
}
}


/// <summary>
/// 新建一个新节点(用户名,设置)
/// </summary>
private XmlNode getIPXmlNode(string elemName, string[] data)
{
XmlElement xmlelem = xmld.CreateElement(elemName);
for (int i = 0; i < strName.Length; i++)
{
XmlElement xmle = xmld.CreateElement(strName[i]);
xmle.InnerText = data[i];
xmlelem.AppendChild(xmle);
}
return (XmlNode)xmlelem;
}


/// <summary>
/// 将节点添加到xml文件中
/// </summary>
private void addIPXmlNode(XmlNode xmln)
{
xmld.Load(fileName);
XmlNode xmlRoot = xmld.SelectSingleNode("AddressList");
XmlNode xmln_ = xmlRoot.OwnerDocument.ImportNode(xmln, true);
xmlRoot.AppendChild(xmln_);
xmld.Save(fileName);

}

private void delIPXmlNode(string userName)
{
xmld.Load(fileName);
XmlNode xmlRoot = xmld.SelectSingleNode("AddressList");
XmlNodeList xmlnlist = xmlRoot.ChildNodes;

foreach (XmlNode xn in xmlnlist)
{
if (userName.Equals(xn.Name))
xmlRoot.RemoveChild(xn);
}
xmld.Save(fileName);
}

}

保存IP数据的:initIP.xml

<AddressList>
  <Lanix>
    <IP>172.16.132.100</IP>
    <Subnet>255.255.255.0</Subnet>
    <Gateway>172.16.132.1</Gateway>
    <DNS>202.112.80.106</DNS>
    <CopyDNS>202.112.80.168</CopyDNS>
  </Lanix>
  <Bingil>
    <IP>172.16.132.99</IP>
    <Subnet>255.255.255.0</Subnet>
    <Gateway>172.16.132.1</Gateway>
    <DNS>202.112.80.106</DNS>
    <CopyDNS>202.112.80.168</CopyDNS>
  </Bingil>
</AddressList>

新建的控件:IpTab.cs

    partial class IpTab
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Component Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.grpIP = new System.Windows.Forms.GroupBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.grpDNS = new System.Windows.Forms.GroupBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox6 = new System.Windows.Forms.TextBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.button3 = new System.Windows.Forms.Button();
this.button4 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.grpIP.SuspendLayout();
this.grpDNS.SuspendLayout();
this.SuspendLayout();
//
// grpIP
//
this.grpIP.Controls.Add(this.textBox3);
this.grpIP.Controls.Add(this.textBox2);
this.grpIP.Controls.Add(this.textBox1);
this.grpIP.Controls.Add(this.label3);
this.grpIP.Controls.Add(this.label2);
this.grpIP.Controls.Add(this.label1);
this.grpIP.Location = new System.Drawing.Point(10, 41);
this.grpIP.Name = "grpIP";
this.grpIP.Size = new System.Drawing.Size(302, 148);
this.grpIP.TabIndex = 0;
this.grpIP.TabStop = false;
this.grpIP.Text = "使用下面IP地址";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(155, 102);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(141, 21);
this.textBox3.TabIndex = 5;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(155, 70);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(141, 21);
this.textBox2.TabIndex = 4;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(155, 31);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(141, 21);
this.textBox1.TabIndex = 3;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(22, 105);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(77, 12);
this.label3.TabIndex = 2;
this.label3.Text = "默认网关(D)";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(22, 68);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(83, 12);
this.label2.TabIndex = 1;
this.label2.Text = "子网掩码(U)";
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(22, 31);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(71, 12);
this.label1.TabIndex = 0;
this.label1.Text = "IP 地址(I)";
//
// grpDNS
//
this.grpDNS.Controls.Add(this.textBox5);
this.grpDNS.Controls.Add(this.textBox4);
this.grpDNS.Controls.Add(this.label5);
this.grpDNS.Controls.Add(this.label4);
this.grpDNS.Location = new System.Drawing.Point(10, 207);
this.grpDNS.Name = "grpDNS";
this.grpDNS.Size = new System.Drawing.Size(302, 115);
this.grpDNS.TabIndex = 1;
this.grpDNS.TabStop = false;
this.grpDNS.Text = "使用下面DSN服务器地址";
//
// textBox5
//
this.textBox5.Location = new System.Drawing.Point(155, 68);
this.textBox5.Name = "textBox5";
this.textBox5.Size = new System.Drawing.Size(141, 21);
this.textBox5.TabIndex = 3;
//
// textBox4
//
this.textBox4.Location = new System.Drawing.Point(155, 36);
this.textBox4.Name = "textBox4";
this.textBox4.Size = new System.Drawing.Size(141, 21);
this.textBox4.TabIndex = 2;
//
// label5
//
this.label5.AutoSize = true;
this.label5.Location = new System.Drawing.Point(22, 77);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(125, 12);
this.label5.TabIndex = 1;
this.label5.Text = "备选 DNS 服务器(A)";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(22, 39);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(125, 12);
this.label4.TabIndex = 0;
this.label4.Text = "首选 DNS 服务器(P)";
//
// button1
//
this.button1.Location = new System.Drawing.Point(10, 370);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(49, 23);
this.button1.TabIndex = 2;
this.button1.Text = "当前";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(94, 370);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(49, 23);
this.button2.TabIndex = 3;
this.button2.Text = "应用";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox6
//
this.textBox6.Enabled = false;
this.textBox6.Location = new System.Drawing.Point(165, 335);
this.textBox6.Name = "textBox6";
this.textBox6.Size = new System.Drawing.Size(141, 21);
this.textBox6.TabIndex = 7;
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(47, 340);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(71, 16);
this.radioButton1.TabIndex = 6;
this.radioButton1.TabStop = true;
this.radioButton1.Text = "IP页面名";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// button3
//
this.button3.Enabled = false;
this.button3.Location = new System.Drawing.Point(262, 370);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(49, 23);
this.button3.TabIndex = 4;
this.button3.Text = "添加";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// button4
//
this.button4.Enabled = false;
this.button4.Location = new System.Drawing.Point(178, 370);
this.button4.Name = "button4";
this.button4.Size = new System.Drawing.Size(49, 23);
this.button4.TabIndex = 5;
this.button4.Text = "删除";
this.button4.UseVisualStyleBackColor = true;
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// comboBox1
//
this.comboBox1.Location = new System.Drawing.Point(22, 11);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(284, 20);
this.comboBox1.TabIndex = 8;
//
// IpTab
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.textBox6);
this.Controls.Add(this.radioButton1);
this.Controls.Add(this.button4);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.grpDNS);
this.Controls.Add(this.grpIP);
this.Name = "IpTab";
this.Size = new System.Drawing.Size(327, 405);
this.Load += new System.EventHandler(this.IpTab_Load);
this.grpIP.ResumeLayout(false);
this.grpIP.PerformLayout();
this.grpDNS.ResumeLayout(false);
this.grpDNS.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.GroupBox grpIP;
private System.Windows.Forms.GroupBox grpDNS;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox6;
private System.Windows.Forms.RadioButton radioButton1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4;
private System.Windows.Forms.ComboBox comboBox1;
}


public partial class IpTab : UserControl
{
private MainForm parentForm;
public IpTab(MainForm parentForm)
{
InitializeComponent();
this.parentForm = parentForm;
}
public void changeStatus()
{
if (this.Parent.Text != "首页")
{
this.button1.Enabled = false;
this.button4.Enabled = true;
}
}

private void button1_Click(object sender, EventArgs e)
{
IpChangerByRegistry ipChanger = new IpChangerByRegistry();
IpGroup ipg = new IpGroup("No setting");
IpGroup ipg1;
try
{
if (this.comboBox1.SelectedIndex == 0)
{
for (int i = 0; i < ipChanger.NetWorkCards.Count; i++)
{
string key = ipChanger.NetWorkCards.ElementAt(i).Key;
ipg1 = ipChanger.getTcpIp(key);
if (ipg.settingCount() < ipg1.settingCount())
ipg = ipg1;
}
}
else
{
ipg = ipChanger.getTcpIp(this.comboBox1.Text);
}
}
catch (Exception)
{
ipg = new IpGroup("No setting");
}
setIpGroup(ipg);
}

private void button2_Click(object sender, EventArgs e)
{
IpChangerByRegistry ipChanger = new IpChangerByRegistry();
IpGroup ipg = getIpGroup();
try
{
if (this.comboBox1.SelectedIndex == 0)
{
for (int i = 0; i < ipChanger.NetWorkCards.Count; i++)
{
string key = ipChanger.NetWorkCards.ElementAt(i).Key;
ipChanger.setTcpIp(key, ipg);
}
}
else
{
ipChanger.setTcpIp(this.comboBox1.Text,ipg);
}

}
catch (Exception)
{
return;
}
}

private IpGroup getIpGroup()
{
IpGroup ipg = new IpGroup("No setting");
ipg.IpAddress = this.textBox1.Text;
ipg.IpSubnetMask = this.textBox2.Text;
ipg.IpGateway = this.textBox3.Text;
ipg.FirstDns = this.textBox4.Text;
ipg.SecondDns = this.textBox5.Text;
return ipg;
}

private void setIpGroup(IpGroup ipg)
{
this.textBox1.Text = ipg.IpAddress;
this.textBox2.Text = ipg.IpSubnetMask;
this.textBox3.Text = ipg.IpGateway;
this.textBox4.Text = ipg.FirstDns;
this.textBox5.Text = ipg.SecondDns;
}

private void IpTab_Load(object sender, EventArgs e)
{
IpChangerByRegistry ipChanger = new IpChangerByRegistry();
this.comboBox1.Items.Add("Default all NetworkCard");
try
{
for (int i = 0; i < ipChanger.NetWorkCards.Count; i++)
{
this.comboBox1.Items.Add(ipChanger.NetWorkCards.ElementAt(i).Key);
}
}
catch (Exception)
{
return;
}
this.comboBox1.SelectedIndex = 0;
}

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
this.textBox6.Enabled = true;
this.button3.Enabled = true;
}
public void initValuve(string[] strArr)
{
this.textBox1.Text = strArr[0];
this.textBox2.Text = strArr[1];
this.textBox3.Text = strArr[2];
this.textBox4.Text = strArr[3];
this.textBox5.Text = strArr[4];
}

private void button3_Click(object sender, EventArgs e)
{
string elemName = this.textBox6.Text;
if(String.IsNullOrWhiteSpace(elemName))
{
MessageBox.Show("用户名无效,请重新输入。");
return;
}
if (parentForm.IpXml.Hst.ContainsKey(elemName))
{
MessageBox.Show("用户名已存在,请重新输入。");
return;
}
string[] data = new string[5];
data[0] = this.textBox1.Text;
data[1] = this.textBox2.Text;
data[2] = this.textBox3.Text;
data[3] = this.textBox4.Text;
data[4] = this.textBox5.Text;

parentForm.IpXml.synbiosis(elemName, data);

TabControl tabC = (TabControl)this.Parent.Parent;
TabPage tabp = new TabPage();
tabp.Text = elemName;
tabC.Controls.Add(tabp);
IpTab ipt = new IpTab(this.parentForm);
tabp.Controls.Add(ipt);
ipt.initValuve(data);
ipt.changeStatus();
tabC.SelectedIndex = tabC.Controls.Count - 1;
}

private void button4_Click(object sender, EventArgs e)
{
TabPage tabp = (TabPage)this.Parent;
string userName = tabp.Text;
parentForm.IpXml.synbiosis(userName);
TabControl tabC = (TabControl)tabp.Parent;
tabC.Controls.Remove(tabp);
}

}

主界面:MainForm.cs

    partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows Form Designer generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.tabPage1 = new System.Windows.Forms.TabPage();
this.ipTab1 = new IPChanger.IpTab(this);
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1.SuspendLayout();
this.tabControl1.SuspendLayout();
this.SuspendLayout();

//
// tabPage1
//
this.tabPage1.Controls.Add(this.ipTab1);
this.tabPage1.Location = new System.Drawing.Point(4, 22);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(329, 376);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "首页";
this.tabPage1.UseVisualStyleBackColor = true;
//
// ipTab1
//
this.ipTab1.Dock = System.Windows.Forms.DockStyle.Fill;
this.ipTab1.Location = new System.Drawing.Point(3, 3);
this.ipTab1.Name = "ipTab1";
this.ipTab1.Size = new System.Drawing.Size(323, 370);
this.ipTab1.TabIndex = 0;
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 0);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(337, 402);
this.tabControl1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(337, 430);
this.Controls.Add(this.tabControl1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "IP更改器";
this.TopMost = true;
this.tabPage1.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TabPage tabPage1;
private IpTab ipTab1;
private System.Windows.Forms.TabControl tabControl1;

}

public partial class MainForm : Form
{
Hashtable hst;
TabPage[] tabpages;
IpTab[] Iptabs;
IPXmlHandler ipXml;

internal IPXmlHandler IpXml
{
get { return ipXml; }
set { ipXml = value; }
}

public MainForm()
{

InitializeComponent();
init();

}

/// <summary>
/// 初始化
/// </summary>
private void init()
{
initHashtable();
initTabpages();
addTabpages();
}

/// <summary>
/// 从xmlIP.xml中读取数据,存入哈希表中
/// </summary>
private void initHashtable()
{
string xmlFile = System.AppDomain.CurrentDomain.BaseDirectory;
xmlFile = xmlFile + "initIP.xml";
ipXml = new IPXmlHandler(xmlFile);
hst = new Hashtable();
hst = ipXml.Hst;
}

/// <summary>
/// 初始化页面
/// </summary>
private void initTabpages()
{
Iptabs = new IpTab[hst.Count];
tabpages = new TabPage[hst.Count];

int i = 0;
foreach (DictionaryEntry de in hst)
{
string[] val = (string[])de.Value;
string key = (string)de.Key;
Iptabs[i] = new IpTab(this);
Iptabs[i].initValuve(val);

tabpages[i] = new TabPage();
tabpages[i].Text = key;
tabpages[i].Controls.Add(Iptabs[i]);
Iptabs[i].changeStatus();
i++;
}
}

/// <summary>
/// 将页面添加入窗体
/// </summary>
private void addTabpages()
{
foreach (TabPage tp in tabpages)
{
this.tabControl1.Controls.Add(tp);
}
}

}







 

 


posted @ 2012-04-02 00:14  lanix  阅读(1279)  评论(0编辑  收藏  举报