积极答复者
如何 修改IP 请教各位

问题
-
我用 Vb008 做个程序 请教如何修改IP DNS 啊 谢谢 赐教
- 已移动 Sheng Jiang 蒋晟Moderator 2010年10月10日 14:58 (发件人:Visual Basic)
答案
-
Imports System.Management
Namespace ArLi.CommonPrj
Public Class ChangeIP''' <summary>
''' Build of ArLi 2003.6.3
''' </summary>
Public Shared ReadOnly myVersion As New System.Version(1, 1)Private iObj As ManagementBaseObject = Nothing
Private oObj As ManagementBaseObject = Nothing
Private mc As New ManagementClass("Win32_NetworkAdapterConfiguration ")
Private ReadOnly moc As ManagementObjectCollection''' <summary>
''' 例:
''' <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 Sub New()
moc = mc.GetInstances()
End Sub''' <summary> 控制连接 </summary>
''' <param name= "ipAddr "> IP地址列表 </param>
''' <param name= "subnetMask "> 对应子网掩码列表 </param>
Public Sub ChangeTo(ipAddr As String(), subnetMask As String())
For Each mo As ManagementObject In moc
If Not CBool(mo("IPEnabled ")) Then
Continue For
End IfiObj = mo.GetMethodParameters("EnableStatic ")
iObj("IPAddress ") = ipAddr
iObj("SubnetMask ") = subnetMask
oObj = mo.InvokeMethod("EnableStatic ", iObj, Nothing)
Next
End Sub''' <summary> 控制连接 </summary>
''' <param name= "ipAddr "> IP地址列表 </param>
''' <param name= "subnetMask "> 对应子网掩码列表 </param>
''' <param name= "gateways "> 网关列表 </param>
''' <param name= "gatewayCostMetric "> 网关接口跃点数列表 </param>
Public Sub ChangeTo(ipAddr As String(), subnetMask As String(), gateways As String(), gatewayCostMetric As String())
For Each mo As ManagementObject In moc
If Not CBool(mo("IPEnabled ")) Then
Continue For
End IfiObj = mo.GetMethodParameters("EnableStatic ")
iObj("IPAddress ") = ipAddr
iObj("SubnetMask ") = subnetMask
oObj = mo.InvokeMethod("EnableStatic ", iObj, Nothing)iObj = mo.GetMethodParameters("SetGateways ")
iObj("DefaultIPGateway ") = gateways
iObj("GatewayCostMetric ") = gatewayCostMetric
oObj = mo.InvokeMethod("SetGateways ", iObj, Nothing)
Next
End Sub''' <summary> 控制连接 </summary>
''' <param name= "ipAddr "> IP地址列表 </param>
''' <param name= "subnetMask "> 对应子网掩码列表 </param>
''' <param name= "gateways "> 网关列表 </param>
''' <param name= "gatewayCostMetric "> 网关接口跃点数列表 </param>
''' <param name= "dnsServer "> DNS 服务器列表 </param>
Public Sub ChangeTo(ipAddr As String(), subnetMask As String(), gateways As String(), gatewayCostMetric As String(), dnsServer As String())
For Each mo As ManagementObject In moc
If Not CBool(mo("IPEnabled ")) Then
Continue For
End IfiObj = mo.GetMethodParameters("EnableStatic ")
iObj("IPAddress ") = ipAddr
iObj("SubnetMask ") = subnetMask
oObj = mo.InvokeMethod("EnableStatic ", iObj, Nothing)iObj = mo.GetMethodParameters("SetGateways ")
iObj("DefaultIPGateway ") = gateways
iObj("GatewayCostMetric ") = gatewayCostMetric
oObj = mo.InvokeMethod("SetGateways ", iObj, Nothing)iObj = mo.GetMethodParameters("SetDNSServerSearchOrder ")
iObj("DNSServerSearchOrder ") = dnsServer
oObj = mo.InvokeMethod("SetDNSServerSearchOrder ", iObj, Nothing)
Next
End Sub''' <summary> 控制连接,使它使用 DHCP </summary>
Public Sub EnableDHCP()
For Each mo As ManagementObject In moc
If Not CBool(mo("IPEnabled ")) Then
Continue For
End IfIf Not CBool(mo("DHCPEnabled ")) Then
iObj = mo.GetMethodParameters("EnableDHCP ")
oObj = mo.InvokeMethod("EnableDHCP ", iObj, Nothing)
End If
Next
End Sub
End Class
End Namespace- 已标记为答案 ThankfulHeartModerator 2010年10月15日 5:40
全部回复
-
我用 Vb008 做个程序 请教如何修改IP DNS 啊 谢谢 赐教
hi,1.使用WMI修改IP,可参考以下http://www.yongfa365.com/Item/Use-WMi-Change-IP-VBS-yongfa365.html
2.使用NetworkInterface 类,可参考以下http://msdn.microsoft.com/zh-tw/library/system.net.networkinformation.networkinterface.aspx
http://flyfish0007.blog.hexun.com/49145318_d.html
秘訣無它,唯勤而已 -
什么操作系统?Windows Mobile? Windows Phone 7? Windows? Linux?
The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP -
Imports System.Management
Namespace ArLi.CommonPrj
Public Class ChangeIP''' <summary>
''' Build of ArLi 2003.6.3
''' </summary>
Public Shared ReadOnly myVersion As New System.Version(1, 1)Private iObj As ManagementBaseObject = Nothing
Private oObj As ManagementBaseObject = Nothing
Private mc As New ManagementClass("Win32_NetworkAdapterConfiguration ")
Private ReadOnly moc As ManagementObjectCollection''' <summary>
''' 例:
''' <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 Sub New()
moc = mc.GetInstances()
End Sub''' <summary> 控制连接 </summary>
''' <param name= "ipAddr "> IP地址列表 </param>
''' <param name= "subnetMask "> 对应子网掩码列表 </param>
Public Sub ChangeTo(ipAddr As String(), subnetMask As String())
For Each mo As ManagementObject In moc
If Not CBool(mo("IPEnabled ")) Then
Continue For
End IfiObj = mo.GetMethodParameters("EnableStatic ")
iObj("IPAddress ") = ipAddr
iObj("SubnetMask ") = subnetMask
oObj = mo.InvokeMethod("EnableStatic ", iObj, Nothing)
Next
End Sub''' <summary> 控制连接 </summary>
''' <param name= "ipAddr "> IP地址列表 </param>
''' <param name= "subnetMask "> 对应子网掩码列表 </param>
''' <param name= "gateways "> 网关列表 </param>
''' <param name= "gatewayCostMetric "> 网关接口跃点数列表 </param>
Public Sub ChangeTo(ipAddr As String(), subnetMask As String(), gateways As String(), gatewayCostMetric As String())
For Each mo As ManagementObject In moc
If Not CBool(mo("IPEnabled ")) Then
Continue For
End IfiObj = mo.GetMethodParameters("EnableStatic ")
iObj("IPAddress ") = ipAddr
iObj("SubnetMask ") = subnetMask
oObj = mo.InvokeMethod("EnableStatic ", iObj, Nothing)iObj = mo.GetMethodParameters("SetGateways ")
iObj("DefaultIPGateway ") = gateways
iObj("GatewayCostMetric ") = gatewayCostMetric
oObj = mo.InvokeMethod("SetGateways ", iObj, Nothing)
Next
End Sub''' <summary> 控制连接 </summary>
''' <param name= "ipAddr "> IP地址列表 </param>
''' <param name= "subnetMask "> 对应子网掩码列表 </param>
''' <param name= "gateways "> 网关列表 </param>
''' <param name= "gatewayCostMetric "> 网关接口跃点数列表 </param>
''' <param name= "dnsServer "> DNS 服务器列表 </param>
Public Sub ChangeTo(ipAddr As String(), subnetMask As String(), gateways As String(), gatewayCostMetric As String(), dnsServer As String())
For Each mo As ManagementObject In moc
If Not CBool(mo("IPEnabled ")) Then
Continue For
End IfiObj = mo.GetMethodParameters("EnableStatic ")
iObj("IPAddress ") = ipAddr
iObj("SubnetMask ") = subnetMask
oObj = mo.InvokeMethod("EnableStatic ", iObj, Nothing)iObj = mo.GetMethodParameters("SetGateways ")
iObj("DefaultIPGateway ") = gateways
iObj("GatewayCostMetric ") = gatewayCostMetric
oObj = mo.InvokeMethod("SetGateways ", iObj, Nothing)iObj = mo.GetMethodParameters("SetDNSServerSearchOrder ")
iObj("DNSServerSearchOrder ") = dnsServer
oObj = mo.InvokeMethod("SetDNSServerSearchOrder ", iObj, Nothing)
Next
End Sub''' <summary> 控制连接,使它使用 DHCP </summary>
Public Sub EnableDHCP()
For Each mo As ManagementObject In moc
If Not CBool(mo("IPEnabled ")) Then
Continue For
End IfIf Not CBool(mo("DHCPEnabled ")) Then
iObj = mo.GetMethodParameters("EnableDHCP ")
oObj = mo.InvokeMethod("EnableDHCP ", iObj, Nothing)
End If
Next
End Sub
End Class
End Namespace- 已标记为答案 ThankfulHeartModerator 2010年10月15日 5:40