首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

怎么获取本机ip地址?具体说明如下

2012-03-16 
如何获取本机ip地址?具体说明如下!为了方便人家设置ip地址。故写一个可以设置ip地址的界面而且可以获取ip地

如何获取本机ip地址?具体说明如下!
为了方便人家设置ip地址。故写一个可以设置ip地址的界面而且可以获取ip地址。和利用网上邻居属性设置ip地址一样的功能。
1、可以设置自动获取ip地址
2、可以手动设置ip地址,输入栏位最好也是       .         .       .     .也要有子网掩码,网关,DNS,WINS,等设置。
3、用一个按钮事件,点击开IE浏览器的属性--〉连接对话框。
就这么多啦。希望大家能帮我小弟一忙,给点意见,最好有源码或提供一些资料参考。谢谢各位!!!

[解决办法]
参见msdn中关于DNS的相关说明
[解决办法]
第一二个问题可以使用System.Management类解决
如果是winform的话,需要添加对System.Management.dll的引用

然后使用下面的类就可以实现你的前2个要求:
namespace modify
{
public class ChangeIP
{

public static readonly System.Version myVersion = new System.Version(1, 1);
private ManagementBaseObject iObj = null;
private ManagementBaseObject oObj = null;
private ManagementClass mc = new ManagementClass( "Win32_NetworkAdapterConfiguration ");
private readonly ManagementObjectCollection moc;

/// <summary>
/// example:
/// <code>
/// ArLi.CommonPrj.ChangeIP o = new ArLi.CommonPrj.ChangeIP();
/// string[] ipList = new string[]{ "192.168.0.253 ", "192.168.0.250 "};
/// string[] subnetList = new string[]{ "255.255.255.0 ", "255.255.255.0 "};
/// o.ChangeTo(ipList,subnetList);
/// </code>
/// </summary>
public ChangeIP()
{
moc = mc.GetInstances();
}

/// <summary> cortrol </summary>
/// <param name= "ipAddr "> IPAddr List </param>
/// <param name= "subnetMask "> subnetMask List </param>
public void ChangeTo(string[] ipAddr, string[] subnetMask)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo[ "IPEnabled "]) continue;

iObj = mo.GetMethodParameters( "EnableStatic ");
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);
}
}

/// <summary> cortrol </summary>
/// <param name= "ipAddr "> IPAddr List </param>
/// <param name= "subnetMask "> subnetMask List </param>
/// <param name= "gateways "> gateway List </param>
/// <param name= "gatewayCostMetric "> gateway CostMetric List, example: 1 </param>
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo[ "IPEnabled "]) continue;

iObj = mo.GetMethodParameters( "EnableStatic ");
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);

iObj = mo.GetMethodParameters( "SetGateways ");
iObj[ "DefaultIPGateway "] = gateways;
iObj[ "GatewayCostMetric "] = gatewayCostMetric;
oObj = mo.InvokeMethod( "SetGateways ", iObj, null);
}
}



/// <summary> cortrol </summary>
/// <param name= "ipAddr "> IPAddr List </param>
/// <param name= "subnetMask "> subnetMask List </param>
/// <param name= "gateways "> gateway List </param>
/// <param name= "gatewayCostMetric "> gateway CostMetric List, example: 1 </param>
/// <param name= "dnsServer "> DNSServer List </param>
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] dnsServer)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo[ "IPEnabled "]) continue;

iObj = mo.GetMethodParameters( "EnableStatic ");
iObj[ "IPAddress "] = ipAddr;
iObj[ "SubnetMask "] = subnetMask;
oObj = mo.InvokeMethod( "EnableStatic ", iObj, null);

iObj = mo.GetMethodParameters( "SetGateways ");
iObj[ "DefaultIPGateway "] = gateways;
iObj[ "GatewayCostMetric "] = gatewayCostMetric;
oObj = mo.InvokeMethod( "SetGateways ", iObj, null);

iObj = mo.GetMethodParameters( "SetDNSServerSearchOrder ");
iObj[ "DNSServerSearchOrder "] = dnsServer;
oObj = mo.InvokeMethod( "SetDNSServerSearchOrder ", iObj, null);
}
}

/// <summary> DHCPEnabled </summary>
public void EnableDHCP()
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo[ "IPEnabled "]) continue;

if (!(bool)mo[ "DHCPEnabled "])
{
iObj = mo.GetMethodParameters( "EnableDHCP ");
oObj = mo.InvokeMethod( "EnableDHCP ", iObj, null);
}
}
}
}
}
[解决办法]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;


namespace IP
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label 计算机名;
private System.Windows.Forms.Label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

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.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.label1 = new System.Windows.Forms.Label();
this.计算机名 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 128);
this.button1.Name = "button1 ";
this.button1.TabIndex = 0;
this.button1.Text = "查询 ";
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 48);
this.textBox1.Name = "textBox1 ";
this.textBox1.Size = new System.Drawing.Size(288, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1 ";
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(96, 80);
this.richTextBox1.Name = "richTextBox1 ";
this.richTextBox1.Size = new System.Drawing.Size(280, 72);
this.richTextBox1.TabIndex = 3;
this.richTextBox1.Text = "richTextBox2 ";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 8);
this.label1.Name = "label1 ";
this.label1.Size = new System.Drawing.Size(384, 24);
this.label1.TabIndex = 4;
this.label1.Text = "label1 ";
//
// 计算机名
//
this.计算机名.Location = new System.Drawing.Point(16, 56);
this.计算机名.Name = "计算机名 ";
this.计算机名.Size = new System.Drawing.Size(64, 16);
this.计算机名.TabIndex = 5;
this.计算机名.Text = "计算机名 ";
//
// label2
//
this.label2.Location = new System.Drawing.Point(16, 96);
this.label2.Name = "label2 ";
this.label2.Size = new System.Drawing.Size(64, 23);
this.label2.TabIndex = 6;
this.label2.Text = "本地IP ";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(400, 190);
this.Controls.Add(this.label2);
this.Controls.Add(this.计算机名);
this.Controls.Add(this.label1);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1 ";
this.Text = "Form1 ";
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{

}

private void button1_Click_1(object sender, System.EventArgs e)
{
IPHostEntry myHost = new IPHostEntry();
try
{
this.richTextBox1.Text = " ";

myHost = Dns.GetHostByName(Dns.GetHostName());
textBox1.Text = myHost.HostName.ToString();
this.label1.Text= "给你参考一下吧!我也是初学者。不妨交流一下QQ:305660067 ";
for(int i=0; i <myHost.AddressList.Length;i++)
{
richTextBox1.AppendText( "本地主机IP地址-> " + myHost.AddressList[i].ToString()+ " ");
}
}
catch(Exception error)
{


MessageBox.Show(error.Message);
}
}
}
}
[解决办法]
你可以用SqL Server的管理器试下可否在家中看到相应的服务器名,如果不可以看到的话,应该是SQL的设置问题或防火墙等其它因素

可参看如下链接:
http://www.baidu.com/s?wd=SQLDMO.ApplicationClass&cl=3

热点排行