winform如何设置IP地址DNS和网关等?

pyz8000 2006-03-21 08:52:45
机子常在两个网线间换来换去,每换一次都要改这些,好麻烦,想自己做个程序,点一下就换,
不知道有没这方面的函数?

望赐代码:D
...全文
573 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
SpeedyHorse 2006-06-30
  • 打赏
  • 举报
回复
using System.Management;
WMI怎样下载?
谢谢了,新手
mooniscrazy 2006-06-30
  • 打赏
  • 举报
回复
一个网卡可以绑定多个ip.不用写程序。在网上邻居网卡配置的高级属性里面就有。
江城老温 2006-06-21
  • 打赏
  • 举报
回复
mark
marvelstack 2006-06-21
  • 打赏
  • 举报
回复
使用wmi,
http://www.codeproject.com/csharp/wmi.asp
ju_feng 2006-03-21
  • 打赏
  • 举报
回复
的确 用netsh interface dump >> file.dat 备份
用 netsh exec file.dat 恢复
ReViSion 2006-03-21
  • 打赏
  • 举报
回复
哈哈,我也收下啦
ip663333 2006-03-21
  • 打赏
  • 举报
回复
我靠 这个也要写程序啊

做个批处理 就可以了
qyfjl 2006-03-21
  • 打赏
  • 举报
回复
// 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

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[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 = "";
}
}
}
qyfjl 2006-03-21
  • 打赏
  • 举报
回复
//
// 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;
//
qyfjl 2006-03-21
  • 打赏
  • 举报
回复
调用WMI来实现.下面代码就行.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management;

namespace 修改网络设置
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
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;
/// <summary>
/// 必需的设计器变量。
/// </summary>
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()
{
//
// 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.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地址:";
我看你有戏 2006-03-21
  • 打赏
  • 举报
回复
肯定有的
LGame 2006-03-21
  • 打赏
  • 举报
回复
好像WWW。CODEPROJECT。COM上有!

我好像以前看到过这样的贴子!

但忘记了,

也找不到了,

只好帮你顶了

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧