如何获取本机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);
}
}