版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、C#制作IP地址切换的源程序代码1.启动代码:12.主界面设计器代码:13.主界面程序代码:74.Xml文件操作代码:141. 启动代码:using System;using System.Collections.Generic;using System.Windows.Forms;namespace IpChange static class Program / <summary> / 应用程序的主入口点。 / </summary> STAThread static void Main() Application.EnableVisualStyles(); Appli
2、cation.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); 2. 主界面设计器代码:namespace IpChange partial class Form1 / <summary> / 必需的设计器变量。 / </summary> private System.ComponentModel.IContainer components = null; / <summary> / 清理所有正在使用的资源。 / </summary> / <para
3、m name="disposing">如果应释放托管资源,为 true;否则为 false。</param> protected override void Dispose(bool disposing) if (disposing && (components != null) components.Dispose(); base.Dispose(disposing); #region Windows 窗体设计器生成的代码 / <summary> / 设计器支持所需的方法 - 不要 / 使用代码编辑器修改此方法的内容。 / &
4、lt;/summary> private void InitializeComponent() this.pnlNetworkInfo = new System.Windows.Forms.Panel(); this.dgvNetwork = new System.Windows.Forms.DataGridView(); this.panel1 = new System.Windows.Forms.Panel(); this.btnSetIP = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Fo
5、rms.Label(); this.btnSaveLocal = new System.Windows.Forms.Button(); this.pnlIpInfo = new System.Windows.Forms.Panel(); this.dgvLocalXml = new System.Windows.Forms.DataGridView(); this.panel2 = new System.Windows.Forms.Panel(); this.label3 = new System.Windows.Forms.Label(); this.txtLocalIpName = new
6、 System.Windows.Forms.TextBox(); this.label2 = new System.Windows.Forms.Label(); this.pnlNetworkInfo.SuspendLayout(); (System.ComponentModel.ISupportInitialize)(this.dgvNetwork).BeginInit(); this.panel1.SuspendLayout(); this.pnlIpInfo.SuspendLayout(); (System.ComponentModel.ISupportInitialize)(this.
7、dgvLocalXml).BeginInit(); this.panel2.SuspendLayout(); this.SuspendLayout(); / / pnlNetworkInfo / this.pnlNetworkInfo.Controls.Add(this.dgvNetwork); this.pnlNetworkInfo.Controls.Add(this.panel1); this.pnlNetworkInfo.Dock = System.Windows.Forms.DockStyle.Top; this.pnlNetworkInfo.Location = new System
8、.Drawing.Point(0, 0); this.pnlNetworkInfo.Name = "pnlNetworkInfo" this.pnlNetworkInfo.Size = new System.Drawing.Size(572, 157); this.pnlNetworkInfo.TabIndex = 0; / / dgvNetwork / this.dgvNetwork.AllowUserToAddRows = false; this.dgvNetwork.AllowUserToDeleteRows = false; this.dgvNetwork.Auto
9、SizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells; this.dgvNetwork.BackgroundColor = System.Drawing.SystemColors.Control; this.dgvNetwork.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dgvNetwork.Dock = System.W
10、indows.Forms.DockStyle.Fill; this.dgvNetwork.Location = new System.Drawing.Point(0, 30); this.dgvNetwork.Name = "dgvNetwork" this.dgvNetwork.ReadOnly = true; this.dgvNetwork.RowTemplate.Height = 23; this.dgvNetwork.Size = new System.Drawing.Size(572, 127); this.dgvNetwork.TabIndex = 1; thi
11、s.dgvNetwork.MouseClick += new System.Windows.Forms.MouseEventHandler(this.dgvNetwork_MouseClick); / / panel1 / this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.panel1.Controls.Add(this.btnSetIP); this.panel1.Controls.Add(this.label1); this.panel1.Dock = System.Windows.Fo
12、rms.DockStyle.Top; this.panel1.Location = new System.Drawing.Point(0, 0); this.panel1.Name = "panel1" this.panel1.Size = new System.Drawing.Size(572, 30); this.panel1.TabIndex = 0; / / btnSetIP / this.btnSetIP.Location = new System.Drawing.Point(437, 4); this.btnSetIP.Name = "btnSetIP
13、" this.btnSetIP.Size = new System.Drawing.Size(111, 23); this.btnSetIP.TabIndex = 2; this.btnSetIP.Text = "使用备用IP信息" this.btnSetIP.UseVisualStyleBackColor = true; this.btnSetIP.Click += new System.EventHandler(this.btnSetIP_Click); / / label1 / this.label1.AutoSize = true; this.label1
14、.Location = new System.Drawing.Point(12, 9); this.label1.Name = "label1" this.label1.Size = new System.Drawing.Size(65, 12); this.label1.TabIndex = 0; this.label1.Text = "网卡信息:" / / btnSaveLocal / this.btnSaveLocal.Location = new System.Drawing.Point(437, 3); this.btnSaveLocal.Na
15、me = "btnSaveLocal" this.btnSaveLocal.Size = new System.Drawing.Size(111, 23); this.btnSaveLocal.TabIndex = 1; this.btnSaveLocal.Text = "IP信息保存到备用" this.btnSaveLocal.UseVisualStyleBackColor = true; this.btnSaveLocal.Click += new System.EventHandler(this.btnSaveLocal_Click); / / p
16、nlIpInfo / this.pnlIpInfo.Controls.Add(this.dgvLocalXml); this.pnlIpInfo.Controls.Add(this.panel2); this.pnlIpInfo.Dock = System.Windows.Forms.DockStyle.Fill; this.pnlIpInfo.Location = new System.Drawing.Point(0, 157); this.pnlIpInfo.Name = "pnlIpInfo" this.pnlIpInfo.Size = new System.Draw
17、ing.Size(572, 281); this.pnlIpInfo.TabIndex = 1; / / dgvLocalXml / this.dgvLocalXml.AllowUserToAddRows = false; this.dgvLocalXml.AllowUserToDeleteRows = false; this.dgvLocalXml.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells; this.dgvLocalXml.BackgroundColor = Sys
18、tem.Drawing.SystemColors.Control; this.dgvLocalXml.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dgvLocalXml.Dock = System.Windows.Forms.DockStyle.Fill; this.dgvLocalXml.Location = new System.Drawing.Point(0, 29); this.dgvLocalXml.Name = &q
19、uot;dgvLocalXml" this.dgvLocalXml.ReadOnly = true; this.dgvLocalXml.RowTemplate.Height = 23; this.dgvLocalXml.Size = new System.Drawing.Size(572, 252); this.dgvLocalXml.TabIndex = 2; this.dgvLocalXml.MouseClick += new System.Windows.Forms.MouseEventHandler(this.dgvLocalXml_MouseClick); / / pane
20、l2 / this.panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.panel2.Controls.Add(this.label3); this.panel2.Controls.Add(this.txtLocalIpName); this.panel2.Controls.Add(this.label2); this.panel2.Controls.Add(this.btnSaveLocal); this.panel2.Dock = System.Windows.Forms.DockStyle.Top
21、; this.panel2.Location = new System.Drawing.Point(0, 0); this.panel2.Name = "panel2" this.panel2.Size = new System.Drawing.Size(572, 29); this.panel2.TabIndex = 3; / / label3 / this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(269, 8); this.label3.Name = "la
22、bel3" this.label3.Size = new System.Drawing.Size(41, 12); this.label3.TabIndex = 3; this.label3.Text = "名称:" / / txtLocalIpName / this.txtLocalIpName.Location = new System.Drawing.Point(311, 5); this.txtLocalIpName.Name = "txtLocalIpName" this.txtLocalIpName.Size = new Syste
23、m.Drawing.Size(100, 21); this.txtLocalIpName.TabIndex = 2; / / label2 / this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 8); this.label2.Name = "label2" this.label2.Size = new System.Drawing.Size(77, 12); this.label2.TabIndex = 1; this.label2.Text = "备用
24、IP信息:" / / Form1 / this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(572, 438); this.Controls.Add(this.pnlIpInfo); this.Controls.Add(this.pnlNetworkInfo); this.MaximizeBox = false
25、; this.Name = "Form1" this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "IPChange" this.pnlNetworkInfo.ResumeLayout(false); (System.ComponentModel.ISupportInitialize)(this.dgvNetwork).EndInit(); this.panel1.ResumeLayout(false); this.panel1.Perf
26、ormLayout(); this.pnlIpInfo.ResumeLayout(false); (System.ComponentModel.ISupportInitialize)(this.dgvLocalXml).EndInit(); this.panel2.ResumeLayout(false); this.panel2.PerformLayout(); this.ResumeLayout(false); #endregion private System.Windows.Forms.Panel pnlNetworkInfo; private System.Windows.Forms.
27、Panel pnlIpInfo; private System.Windows.Forms.Panel panel1; private System.Windows.Forms.Label label1; private System.Windows.Forms.DataGridView dgvNetwork; private System.Windows.Forms.Button btnSaveLocal; private System.Windows.Forms.Button btnSetIP; private System.Windows.Forms.DataGridView dgvLo
28、calXml; private System.Windows.Forms.Panel panel2; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtLocalIpName; 3. 主界面程序代码:using System;using System.Collections;using System.Collections.Generic;using System.ComponentModel;
29、using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.Management;using System.IO;namespace IpChange public partial class Form1 : Form public Form1() InitializeComponent(); if (Init() < 1) MessageBox.Show("未找到网卡!"); / <summar
30、y> / 网卡信息 / </summary> private DataTable _dtNetwork; / <summary> / 选中的网卡信息 / </summary> private DataRow _drNetworkSelected; / <summary> / 选中的本地配置信息 / </summary> private DataRow _drLocalIPSelected; private string _strXmlPath = string.Format("0config.xml", Ap
31、plication.StartupPath); / <summary> / 生成Xml配置文件 / </summary> / <param name="strPath">配置文件路径</param> / <param name="iType">1添加 2修改 3删除</param> / <param name="dr">IP信息</param> / <param name="strIpName">IP名称&l
32、t;/param> private void ConfigXmlOper(string strPath, int iType, DataRow dr,string strIpName) XmlHelper xmls = new XmlHelper(this._strXmlPath); if (!File.Exists(strPath) xmls.CreateXmlRoot("IPInfo", true, true); if (dr != null) /生成属性键值集合 Dictionary<string, string> dXml; string strC
33、hildNoteName = "Notes" /插入IP信息 dXml = new Dictionary<string, string>(); dXml.Add("Name", strIpName); dXml.Add("IP", ConvertObjectToString(dr1); dXml.Add("Mask", ConvertObjectToString(dr2); dXml.Add("GateWay", ConvertObjectToString(dr3); dXml.Ad
34、d("DNS", ConvertObjectToString(dr4); xmls.CreatXmlNode("/IPInfo", strChildNoteName, dXml);/这里MainNode语法 /保存 xmls.XmlSave(); / <summary> / 设置IP信息 / </summary> / <param name="drNetwork"></param> / <param name="drIpInfo"></param&g
35、t; private void SetIP(DataRow drNetwork, DataRow drIpInfo) if (this.dgvNetwork.DataSource != null && this.dgvNetwork.Rows.Count > 0 && drIpInfo != null) if (this.dgvNetwork.Rows.Count = 1)/直接设置到默认网卡 SetNetworkAdapter(ManagementObject)(this.dgvNetwork.Rows0.Cells6.Value), drIpInfo)
36、; else if (drNetwork != null) SetNetworkAdapter(ManagementObject)(drNetwork6), drIpInfo); else MessageBox.Show("请选择需要设置信息的网卡!"); return; private void SetNetworkAdapter(ManagementObject moSelected,DataRow drIP) ManagementBaseObject inPar = null; ManagementBaseObject outPar = null; Managemen
37、tClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection moc = mc.GetInstances(); foreach (ManagementObject mo in moc) if (moSelected.ToString() != mo.ToString() continue; /设置ip地址和子网掩码 inPar = mo.GetMethodParameters("EnableStatic"); inPar&q
38、uot;IPAddress" = new string drIP1.ToString() ; inPar"SubnetMask" = new string drIP2.ToString() ; outPar = mo.InvokeMethod("EnableStatic", inPar, null); /设置网关地址 inPar = mo.GetMethodParameters("SetGateways"); inPar"DefaultIPGateway" = new string drIP3.ToStr
39、ing() ; outPar = mo.InvokeMethod("SetGateways", inPar, null); /设置DNS string strTmp = drIP4.ToString().Split(''); inPar = mo.GetMethodParameters("SetDNSServerSearchOrder"); inPar"DNSServerSearchOrder" = strTmp; outPar = mo.InvokeMethod("SetDNSServerSearchOrd
40、er", inPar, null); MessageBox.Show("切换成功!"); InitNetworkInfo(); break; / <summary> / 初始化 / </summary> / <returns></returns> private int Init() if (InitNetworkInfo() < 1) MessageBox.Show("未找到网卡!"); return -1; if (InitLocalXml() < 1) MessageBox.Sh
41、ow("读取本地配置文件失败!"); return -1; return 1; / <summary> / 初始化网卡信息 / </summary> / <returns></returns> private int InitNetworkInfo() _dtNetwork = new DataTable(); _dtNetwork = new DataTable(); _dtNetwork.Columns.Add("状态", typeof(string); _dtNetwork.Columns.Add(&
42、quot;IP地址", typeof(string); _dtNetwork.Columns.Add("子网掩码", typeof(string); _dtNetwork.Columns.Add("网关", typeof(string); _dtNetwork.Columns.Add("DNS", typeof(string); _dtNetwork.Columns.Add("MAC地址", typeof(string); _dtNetwork.Columns.Add("ManagementOb
43、ject", typeof(object); string strGetNetWorkName = "SELECT * FROM Win32_NetworkAdapterConfiguration" ManagementObjectSearcher query = new ManagementObjectSearcher(strGetNetWorkName); if (query = null) return -1; ManagementObjectCollection queryCollection = query.Get(); if (queryCollection = null) return -1; forea
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年物业外墙涂装清工承包协议3篇
- 2025版股票代持与公司治理结构优化合同3篇
- 2024年网络安全外包服务合同3篇
- 二零二五年型汽车吊机租赁项目合作协议3篇
- 2024年版消防与通风系统建设标准化协议模板版
- 2024年精细化粮食采购合同范本版B版
- 2024年食堂工作人员劳动合同及竞业限制条款3篇
- 2025版婚前婚内夫妻共同财产仲裁协议书范本3篇
- 2025版兼职领队带团旅游合同争议解决机制3篇
- 2025版电子产品研发与采购合同2篇
- 全国城市车牌代码一览表
- 小区物业弱电维护方案
- 典型地铁突发事件应急管理案例分析上海地铁碰撞事故
- 广西柳州市2023-2024学年四年级上学期期末考试语文试卷
- 《芯片制造工艺》课件
- 中山大学研究生中特考试大题
- 手术室护理实践指南术中低体温预防
- 四川省成都市2022-2023学年六年级上学期语文期末考试试卷(含答案)5
- (病理科)提高HE切片优良率PDCA
- Creo-7.0基础教程-配套课件
- 全国火车站编码
评论
0/150
提交评论