我有以下vbscript代码,可以返回本地IP地址。它工作得很好。我尝试在我的winform .net应用程序中提供相同的功能。
我找到的所有解决方案都涉及使用DNS。有没有什么方法可以将这个脚本“移植”到.net中使用?
也许有一种不同的方法来做到这一点?
谢谢!
我找到的所有解决方案都涉及使用DNS。有没有什么方法可以将这个脚本“移植”到.net中使用?
也许有一种不同的方法来做到这一点?
谢谢!
Function GetIP()
Dim ws : Set ws = CreateObject("WScript.Shell")
Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
Dim TmpFile : TmpFile = fso.GetSpecialFolder(2) & "/ip.txt"
Dim ThisLine, IP
If ws.Environment("SYSTEM")("OS") = "" Then
ws.run "winipcfg /batch " & TmpFile, 0, True
Else
ws.run "%comspec% /c ipconfig > " & TmpFile, 0, True
End If
With fso.GetFile(TmpFile).OpenAsTextStream
Do While NOT .AtEndOfStream
ThisLine = .ReadLine
If InStr(ThisLine, "Address") <> 0 Then IP = Mid(ThisLine, InStr(ThisLine, ":") + 2)
Loop
.Close
End With
If IP <> "" Then
If Asc(Right(IP, 1)) = 13 Then IP = Left(IP, Len(IP) - 1)
End If
GetIP = IP
fso.GetFile(TmpFile).Delete
Set fso = Nothing
Set ws = Nothing
End Function
where
子句上加上ni.OperationalStatus == OperationalStatus.Up
,否则你可能会得到一个当前不可用的适配器的 IP。我在一台带有无线和有线适配器的笔记本电脑上发现了这个问题;当关闭无线适配器时,我仍然能够获取到它的 IP。 - Keith Robertson