网页素材: 导航菜单选项卡滑动门焦点图图片切换下拉菜单弹出层Tabs链接提示

您的当前位置: 源码爱好者 » 网页制作教程(编程教程) » C#技术教程

Visual C#读取网络配置信息

  • 标签:网络设置 更新时间:2014-05-05
  • Visual C#读取网络配置信息,模拟Windows系统的网络配置窗口,修改IP/修改DNS、修改子网掩码等。

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Management;
    namespace 修改网络设置
    {
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Label label1;
    		private System.Windows.Forms.Label label2;
    		private System.Windows.Forms.Label label3;
    		private System.Windows.Forms.Button button1;
    		private System.Windows.Forms.Button button2;
    		private System.Windows.Forms.GroupBox groupBox1;
    		private System.Windows.Forms.Label label4;
    		private System.Windows.Forms.GroupBox groupBox2;
    		private System.Windows.Forms.Label label5;
    		private System.Windows.Forms.Label label6;
    		private System.Windows.Forms.Label label7;
    		private System.Windows.Forms.Label label8;
    		private System.ComponentModel.Container components = null;
    		private System.Windows.Forms.TextBox ipAddresses;
    		private System.Windows.Forms.TextBox subnets;
    		private System.Windows.Forms.TextBox gateways;
    		private System.Windows.Forms.TextBox dnses;
    		private System.Windows.Forms.Label currIP;
    		private System.Windows.Forms.Label currMask;
    		private System.Windows.Forms.Label currGateway;
    		private System.Windows.Forms.Label currDNS;
    		public Form1()
    		{
    			InitializeComponent();
    		}
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null)
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    		#region Windows 窗体设计器生成的代码
    		private void InitializeComponent()
    		{
    			this.label1 = new System.Windows.Forms.Label();
    			this.label2 = new System.Windows.Forms.Label();
    			this.label3 = new System.Windows.Forms.Label();
    			this.ipAddresses = new System.Windows.Forms.TextBox();
    			this.subnets = new System.Windows.Forms.TextBox();
    			this.gateways = new System.Windows.Forms.TextBox();
    			this.button1 = new System.Windows.Forms.Button();
    			this.button2 = new System.Windows.Forms.Button();
    			this.groupBox1 = new System.Windows.Forms.GroupBox();
    			this.dnses = new System.Windows.Forms.TextBox();
    			this.label4 = new System.Windows.Forms.Label();
    			this.groupBox2 = new System.Windows.Forms.GroupBox();
    			this.currDNS = new System.Windows.Forms.Label();
    			this.currGateway = new System.Windows.Forms.Label();
    			this.currMask = new System.Windows.Forms.Label();
    			this.currIP = new System.Windows.Forms.Label();
    			this.label8 = new System.Windows.Forms.Label();
    			this.label7 = new System.Windows.Forms.Label();
    			this.label6 = new System.Windows.Forms.Label();
    			this.label5 = new System.Windows.Forms.Label();
    			this.groupBox1.SuspendLayout();
    			this.groupBox2.SuspendLayout();
    			this.SuspendLayout();
    			//
    			// label1
    			//
    			this.label1.AutoSize = true;
    			this.label1.Location = new System.Drawing.Point(16, 24);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(54, 17);
    			this.label1.TabIndex = 0;
    			this.label1.Text = "IP地址:";
    			//
    			// label2
    			//
    			this.label2.AutoSize = true;
    			this.label2.Location = new System.Drawing.Point(16, 56);
    			this.label2.Name = "label2";
    			this.label2.Size = new System.Drawing.Size(66, 17);
    			this.label2.TabIndex = 1;
    			this.label2.Text = "子网掩码:";
    			//
    			// label3
    			//
    			this.label3.AutoSize = true;
    			this.label3.Location = new System.Drawing.Point(16, 88);
    			this.label3.Name = "label3";
    			this.label3.Size = new System.Drawing.Size(42, 17);
    			this.label3.TabIndex = 2;
    			this.label3.Text = "网关:";
    			//
    			// ipAddresses
    			//
    			this.ipAddresses.Location = new System.Drawing.Point(96, 24);
    			this.ipAddresses.Name = "ipAddresses";
    			this.ipAddresses.Size = new System.Drawing.Size(184, 21);
    			this.ipAddresses.TabIndex = 3;
    			this.ipAddresses.Text = "";
    			//
    			// subnets
    			//
    			this.subnets.Location = new System.Drawing.Point(96, 56);
    			this.subnets.Name = "subnets";
    			this.subnets.Size = new System.Drawing.Size(184, 21);
    			this.subnets.TabIndex = 4;
    			this.subnets.Text = "";
    			//
    			// gateways
    			//
    			this.gateways.Location = new System.Drawing.Point(96, 88);
    			this.gateways.Name = "gateways";
    			this.gateways.Size = new System.Drawing.Size(184, 21);
    			this.gateways.TabIndex = 5;
    			this.gateways.Text = "";
    			//
    			// button1
    			//
    			this.button1.Location = new System.Drawing.Point(48, 304);
    			this.button1.Name = "button1";
    			this.button1.TabIndex = 6;
    			this.button1.Text = "修改";
    			this.button1.Click += new System.EventHandler(this.button1_Click);
    			//
    			// button2
    			//
    			this.button2.Location = new System.Drawing.Point(232, 304);
    			this.button2.Name = "button2";
    			this.button2.TabIndex = 7;
    			this.button2.Text = "退出";
    			this.button2.Click += new System.EventHandler(this.button2_Click);
    			//
    			// groupBox1
    			//
    			this.groupBox1.Controls.Add(this.dnses);
    			this.groupBox1.Controls.Add(this.label4);
    			this.groupBox1.Controls.Add(this.subnets);
    			this.groupBox1.Controls.Add(this.gateways);
    			this.groupBox1.Controls.Add(this.label3);
    			this.groupBox1.Controls.Add(this.label1);
    			this.groupBox1.Controls.Add(this.label2);
    			this.groupBox1.Controls.Add(this.ipAddresses);
    			this.groupBox1.Location = new System.Drawing.Point(24, 16);
    			this.groupBox1.Name = "groupBox1";
    			this.groupBox1.Size = new System.Drawing.Size(304, 152);
    			this.groupBox1.TabIndex = 8;
    			this.groupBox1.TabStop = false;
    			this.groupBox1.Text = "网络配置";
    			//
    			// dnses
    			//
    			this.dnses.Location = new System.Drawing.Point(96, 120);
    			this.dnses.Name = "dnses";
    			this.dnses.Size = new System.Drawing.Size(184, 21);
    			this.dnses.TabIndex = 7;
    			this.dnses.Text = "";
    			//
    			// label4
    			//
    			this.label4.AutoSize = true;
    			this.label4.Location = new System.Drawing.Point(16, 120);
    			this.label4.Name = "label4";
    			this.label4.Size = new System.Drawing.Size(35, 17);
    			this.label4.TabIndex = 6;
    			this.label4.Text = "DNS:";
    			//
    			// groupBox2
    			//
    			this.groupBox2.Controls.Add(this.currDNS);
    			this.groupBox2.Controls.Add(this.currGateway);
    			this.groupBox2.Controls.Add(this.currMask);
    			this.groupBox2.Controls.Add(this.currIP);
    			this.groupBox2.Controls.Add(this.label8);
    			this.groupBox2.Controls.Add(this.label7);
    			this.groupBox2.Controls.Add(this.label6);
    			this.groupBox2.Controls.Add(this.label5);
    			this.groupBox2.Location = new System.Drawing.Point(24, 176);
    			this.groupBox2.Name = "groupBox2";
    			this.groupBox2.Size = new System.Drawing.Size(304, 120);
    			this.groupBox2.TabIndex = 9;
    			this.groupBox2.TabStop = false;
    			this.groupBox2.Text = "当前配置";
    			//
    			// currDNS
    			//
    			this.currDNS.Location = new System.Drawing.Point(96, 88);
    			this.currDNS.Name = "currDNS";
    			this.currDNS.Size = new System.Drawing.Size(176, 16);
    			this.currDNS.TabIndex = 7;
    			//
    			// currGateway
    			//
    			this.currGateway.Location = new System.Drawing.Point(96, 64);
    			this.currGateway.Name = "currGateway";
    			this.currGateway.Size = new System.Drawing.Size(184, 24);
    			this.currGateway.TabIndex = 6;
    			//
    			// currMask
    			//
    			this.currMask.Location = new System.Drawing.Point(96, 40);
    			this.currMask.Name = "currMask";
    			this.currMask.Size = new System.Drawing.Size(184, 24);
    			this.currMask.TabIndex = 5;
    			//
    			// currIP
    			//
    			this.currIP.Location = new System.Drawing.Point(96, 16);
    			this.currIP.Name = "currIP";
    			this.currIP.Size = new System.Drawing.Size(184, 16);
    			this.currIP.TabIndex = 4;
    			//
    			// label8
    			//
    			this.label8.AutoSize = true;
    			this.label8.Location = new System.Drawing.Point(16, 88);
    			this.label8.Name = "label8";
    			this.label8.Size = new System.Drawing.Size(35, 17);
    			this.label8.TabIndex = 3;
    			this.label8.Text = "DNS:";
    			//
    			// label7
    			//
    			this.label7.AutoSize = true;
    			this.label7.Location = new System.Drawing.Point(16, 64);
    			this.label7.Name = "label7";
    			this.label7.Size = new System.Drawing.Size(42, 17);
    			this.label7.TabIndex = 2;
    			this.label7.Text = "网关:";
    			//
    			// label6
    			//
    			this.label6.AutoSize = true;
    			this.label6.Location = new System.Drawing.Point(16, 40);
    			this.label6.Name = "label6";
    			this.label6.Size = new System.Drawing.Size(66, 17);
    			this.label6.TabIndex = 1;
    			this.label6.Text = "子网掩码:";
    			//
    			// label5
    			//
    			this.label5.AutoSize = true;
    			this.label5.Location = new System.Drawing.Point(16, 16);
    			this.label5.Name = "label5";
    			this.label5.Size = new System.Drawing.Size(54, 17);
    			this.label5.TabIndex = 0;
    			this.label5.Text = "IP地址:";
    			//
    			// Form1
    			//
    			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    			this.ClientSize = new System.Drawing.Size(352, 341);
    			this.Controls.Add(this.groupBox2);
    			this.Controls.Add(this.groupBox1);
    			this.Controls.Add(this.button2);
    			this.Controls.Add(this.button1);
    			this.Name = "Form1";
    			this.Text = "修改网络设置";
    			this.Load += new System.EventHandler(this.Form1_Load);
    			this.groupBox1.ResumeLayout(false);
    			this.groupBox2.ResumeLayout(false);
    			this.ResumeLayout(false);
    		}
    		#endregion
    		[STAThread]
    		static void Main()
    		{
    			Application.Run(new Form1());
    		}
    		private void button1_Click(object sender, System.EventArgs e)
    		{
    			try
    			{
    				ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    				ManagementObjectCollection moc = mc.GetInstances();
    				foreach(ManagementObject mo in moc)
    				{
    					if(!(bool)mo["IPEnabled"])
    						continue;
    					ManagementBaseObject newIP = mo.GetMethodParameters( "EnableStatic" );
    					ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways");
    					ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
    					newIP["IPAddress"] = new string[] {ipAddresses.Text};
    					newIP["SubnetMask"] = new string[] {subnets.Text};
    					newGateway["DefaultIPGateway"] = new string[]{gateways.Text};
    					newDNS["DNSServerSearchOrder"] = new string[]{dnses.Text};
    					ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null);
    					ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateway, null);
    					ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
    				}
    				// 显示当前网络配置
    				getCurrentConfiguration();
    			}
    			catch(Exception exc)
    			{
    				MessageBox.Show(exc.Message, "提示");
    			}
    		}
    		private void Form1_Load(object sender, System.EventArgs e)
    		{
    			getCurrentConfiguration();
    		}
    		private void button2_Click(object sender, System.EventArgs e)
    		{
    			this.Close();  // 关闭窗体
    		}
    		// 显示当前本机的网络配置
    		private void getCurrentConfiguration()
    		{
    			ClearText();  // 清除原来配置
    			ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
    			ManagementObjectCollection moc = mc.GetInstances();
    			foreach(ManagementObject mo in moc)
    			{
    				if(!(bool)mo["IPEnabled"])
    					continue;
    				// 取得本机IP地址,并显示
    				string[] addresses = (string[]) mo["IPAddress"];
    				foreach(string add in addresses)
    					currIP.Text = currIP.Text + add + " ";
    				// 取得本机子网掩码,并显示
    				string[] subnets = (string[]) mo["IPSubnet"];
    				foreach(string sub in subnets)
    					currMask.Text = currMask.Text + sub + " ";
    				// 取得网关,并显示
    				string[] gateways = (string[])mo["DefaultIPGateway"];
    				foreach(string gateway in gateways)
    					currGateway.Text = currGateway.Text + gateway + " ";
    				// 取得DNS服务器地址,并显示
    				string[] dnsList = (string[])mo["DNSServerSearchOrder"];
    				foreach(string dns in dnsList)
    					currDNS.Text = currDNS.Text + dns + " ";
    			}
    		}
    		// 清除Label上的文本
    		private void ClearText()
    		{
    			currIP.Text = "";
    			currMask.Text = "";
    			currGateway.Text = "";
    			currDNS.Text = "";
    		}
    	}
    }

    完整源码:C# 修改网络设置

  • 已读: 次 收藏本文关闭本文打印本文复制链接
处理 SSI 文件时出错