(200分)VB 获取本机IP和重新获取IP问题

VB或VB.net 都可以,请前面说明是那种语言,不用写的太不杂,只要告诉我怎么实现功能,其余的代码我自己写
(我要实现的目的,判断计算机当前IP是不是符合我要求的IP,如果不是重新获取,知道获取到我需要的IP为止)

1、如何获取本机IP(我网上找来的方法正不正确)
添加winSock控件
Socket(Index).LocalIP

2、(主要问题)如何让计算机重新获取IP(类似执行本地连接 禁用,启用操作)

当然,以上问题如果可以通过别的方法,不如windows命令行啊这些能实现的话也是可以的。如果别的语言,请麻烦帮我写好全部(因为我不会)

万分感谢

DOS获取本机IP(括号内的为注释,输命令时别复制进去)。
方法1:arp -a (Interface:后面的就是你自己的IP地址)
方法2:ipconfig/all (IP Address后面的就是你自己的IP地址)
DOS重新获取IP
cmd /c ipconfig /release (释放IP,不执行这步下一步命令将无效)
cmd /c ipconfig /renew (重新获得IP)
上不了网或IP冲突时可以在记事本里贴入如下代码保存为如下文件名并执行:

重新获取IP.bat
@echo off
@echo 正在重新获取IP,请稍候....
cmd /c ipconfig /release > nul
cmd /c ipconfig /renew > nul
arp -a
@echo "Interface:后面的就是你自己的IP地址"
@echo "按任意建测试网络联通(不断跳说明网络正常)"
pause
ping www.baidu.com -t

本程序在我的XP系统下测试通过。
在执行本程序前请不要绑定网卡IP,否则重新获取IP的二条命令无效。
-------------------------------------------------------
'VB获取本机IP
Private Sub Command1_Click()
Dim winIP As Object
Set winIP = CreateObject("MSWinsock.Winsock")
MsgBox "本机IP:" & winIP.localip
End Sub

-------------------------------------------------------
'VB起用和禁用网络连接

Option Explicit

Private Const NetConnect = &H31

Private Sub Command1_Click() '停用本地连接
Dim blnRelust As Boolean
'把 本地连接换成你要控制的本地连接的名字
blnRelust = ExcNetLinkMenu("本地连接", "停用(&B)")
'xp
If blnRelust Then
Debug.Print "停用成功"
Else
blnRelust = ExcNetLinkMenu("本地连接", "禁用(&B)")
End If
If blnRelust Then
Debug.Print "停用成功"
Else
Debug.Print "停用失败"
End If
End Sub

Private Sub command2_Click() '启用本地连接
'把 本地连接换成你要控制的本地连接的名字
Dim blnRelust As Boolean
blnRelust = ExcNetLinkMenu("本地连接", "启用(&A)")
If blnRelust Then
Debug.Print "启用成功"
Else
Debug.Print "启用失败"
End If
End Sub

'首先引用Microsoft Shell Controls And Automation
'先找到“网络连接”这个虚拟文件夹,然后找到要控制的本地连接对应的folderitem,然后枚举verb,找到需要的verb后,调用verb的DoIt方法

Private Function ExcNetLinkMenu(ByVal AdapterName As String, ByVal MenuName As String) As Boolean
Dim objShell As New Shell32.Shell
Dim objFolder As Shell32.Folder
Dim objFolderItem As Shell32.FolderItem
Dim objShellFolderItem As ShellFolderItem
Dim objFolderItemVerb As Shell32.FolderItemVerb
Dim blnRelust As Boolean
On Error Resume Next
Set objFolder = objShell.NameSpace(NetConnect)
If ObjPtr(objFolder) = 0 Then
ExcNetLinkMenu = False
GoTo Exitfunction
End If
For Each objFolderItem In objFolder.Items '遍历网络连接文件夹集合
If objFolderItem.Name = AdapterName Then
Set objShellFolderItem = objFolderItem
blnRelust = True
Exit For
End If
Next
If blnRelust = False Then
ExcNetLinkMenu = False
GoTo Exitfunction
End If
For Each objFolderItemVerb In objShellFolderItem.Verbs '遍历本地连接的右键菜单
If objFolderItemVerb.Name = MenuName Then
objFolderItemVerb.DoIt
ExcNetLinkMenu = True
Exit For
End If
Next
If blnRelust = False Then ExcNetLinkMenu = False
Exitfunction:
Set objShell = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objShellFolderItem = Nothing
Set objFolderItemVerb = Nothing
End Function
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-03-18
【CBM666 的获取本机IP及电脑与用户名称】
'添加 Command1

Private Sub Command1_Click()
Dim aa$, winIP As Object
aa = aa & "本机电脑名称:" & Environ("computername") & vbCrLf
aa = aa & "本机用户名称:" & Environ("username") & vbCrLf
Set winIP = CreateObject("MSWinsock.Winsock")
MsgBox aa & "本机IP:" & winIP.localip
End Sub
这方法获取IP行我试过,你搜索cbm666也可以查到很多很有用的代码,那人是个VB老程序员了也许能帮到你
第2个回答  2009-03-18
//通过主机名获取局域网IP地址;
public static string [] GetIP(string hostName)
{
string [] IPArray ;
int i =0;
try
{
if(hostName == null)
{
hostName = Dns.GetHostName();
}
IPHostEntry ipHostEntry = Dns.GetHostByName(hostName);//传递计算机名
IPArray = new string[ipHostEntry.AddressList.GetLength(0)];
//获取IP地址

foreach (IPAddress addr in ipHostEntry.AddressList)
{
//获取IP地址
//Console.WriteLine(addr.ToString());
IPArray[i++] = addr.ToString();
}
return IPArray;

}
catch
{
Console.WriteLine("错误的主机名。");
}
return new string[]{""};
}

/// <summary>
/// 设置IP,子网掩码,网关,Dns
/// </summary>
/// <param name="ip"></param>
/// <param name="submask"></param>
/// <param name="getway"></param>
/// <param name="dns"></param>
public static void SetIPAddress(string[] ip, string[] submask, string[] getway, string[] dns)
{
ManagementClass wmi = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = wmi.GetInstances();
ManagementBaseObject inPar = null;
ManagementBaseObject outPar = null;
foreach (ManagementObject mo in moc)
{
//如果没有启用IP设置的网络设备则跳过
if (!(bool) mo["IPEnabled"])
continue;

//设置IP
if (ip != null && submask != null)
{
inPar = mo.GetMethodParameters("EnableStatic");
inPar["IPAddress"] = ip;
inPar["SubnetMask"] = submask;
outPar = mo.InvokeMethod("EnableStatic", inPar, null);
}

//设置网关
if (getway != null)
{
inPar = mo.GetMethodParameters("SetGateways");
inPar["DefaultIPGateway"] = getway;
outPar = mo.InvokeMethod("SetGateways", inPar, null);
}

//设置DNS
if (dns != null)
{
inPar = mo.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = dns;
outPar = mo.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
}
}
}

C#语言 需添加命名空间
using System;
using System.Collections;
using System.Text;
using System.Management;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Net;
第3个回答  2009-03-18
用API函数获取
注意以下:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces
这个键下面有个结构类似这样的键
{A04C0017-B0C6-4E77-8593-EE5193C874D9}
注意只有一个里面是真正的本机IP
具体实现方法就看你自己的了
思路就是这样
当前主流的IP设置改写基本都和这个思路差不多!
第4个回答  2009-03-18
shell "cmd /c ipconfig >> c:\ip.txt" '将本地IP等信息全部输入IP.TXT文本。你将会得到一下类似内容:
==================================

Windows IP Configuration

Ethernet adapter 本地连接 4:

Connection-specific DNS Suffix . : domain

IP Address. . . . . . . . . . . . : 192.168.1.100

Subnet Mask . . . . . . . . . . . : 255.255.255.0

Default Gateway . . . . . . . . . : 192.168.1.1

==================================

open "c:\ip.txt" for input as #1 '打开文件,获取信息
While Not EOF(#1)
Line Input #1, tmp
if left(trim(tmp) ,10)="IP Address" then IP= trim(split(tmp,":")(1)) '得到IP

if left(trim(tmp) ,8)="Ethernet" then Line= mid(trim(tmp),len(trim(tmp))-1,1 ) '得到当前为本地连接几
Wend
close #1

=====================================

然后看看IP是否符合要求,如果不符合执行:

shell "cmd /c ipconfig /release " & line '释放IP
shell "cmd /c ipconfig /renew " & line '重新获得IP

为适配器重新分配IP,再执行获取IP操作,看看IP是否符合要求,如果不符合,重复以上操作。

====================================

以上是大致思路,如果不会再找我吧
第5个回答  2009-03-18
在工程中加入Winsock这个控件,名称用默认值:
在表彰上加入一个命令按钮:command1

Private Sub Command1_Click()
MsgBox Winsock1.LocalIP
End Sub

你点一下这个按钮看一下。
在网络编程中winsock是一个非常有用的控件。