我正在使用cmd使用ipconfig > computer.txt将用户网络规范打印到一个文本文件中。这方面的一个例子是
Windows IP Configuration
Wireless LAN adapter Wireless Network Connection:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::cd7a:50b3:1284:865%12
IPv4 Address. . . . . . . . . . . : 10.0.0.26
Subnet Mask . . . . . . . . . . . : 255.0.0.0
Default Gateway . . . . . . . . . : 10.0.0.1
Ethernet adapter Local Area Connection:
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::5c3a:ae77:81:8cdf%11
IPv4 Address. . . . . . . . . . . : 10.0.0.25
Subnet Mask . . . . . . . . . . . : 255.255.255.0
IPv4 Address. . . . . . . . . . . : 192.168.1.12
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 10.0.0.1我如何使用它来找到一个适配器的默认网关(x行以下,然后:
我不确定这是不是正确的方法。
如果不是,使用vb.net查找子网掩码/默认网关的最佳方法是什么
发布于 2012-07-12 05:33:07
如果格式始终相同,则String.IndexOf和String.Substring是有效的方法:
Dim gateWays = From line In File.ReadLines("C:\Temp\data.txt")
Skip 10
Let gatewayIndex = line.IndexOf("Default Gateway . . . . . . . . . :")
Where gatewayIndex > -1
Select line.Substring(gatewayIndex + "Default Gateway . . . . . . . . . :".Length)
Dim firstGateWay = gateWays.FirstOrDefault您可以使用Enumerable.Skip跳过x行,在本例中为10。
发布于 2012-07-12 06:05:58
这应该是您想要的结果:
Private Function GetGateway(ByVal fileName As String) As String
Dim sr As New System.IO.StreamReader(fileName)
Dim foundEthernet As Boolean = False
Dim gateway As String = ""
Do Until sr.EndOfStream
Dim line As String = sr.ReadLine()
If line.Contains("Ethernet adapter LAN:") OrElse line.Contains("Ethernet adapter Local Area Connection:") Then
foundEthernet = True
End If
If foundEthernet Then
If line.Contains("Default Gateway . . . . . . . . . :") Then
gateway = line.Substring(line.IndexOf(":") + 1).Trim
Exit Do
End If
End If
Loop
sr.Close()
Return gateway
End Function发布于 2012-07-12 06:35:48
此外,正如mellamokb所提到的,如果您在要查询的PC上运行此应用程序,则可以使用
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces来获取这些信息。
https://stackoverflow.com/questions/11441743
复制相似问题