2

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

これを使用して、アダプターのデフォルトゲートウェイの1つを見つけるにはどうすればよいでしょうか(x行下およびその後:

これが正しい方法かどうかはわかりません。

そうでない場合は、vb.netを使用してサブネットマスク/デフォルトゲートウェイを見つけるための最良の方法は何ですか

4

3 に答える 3

1

フォーマットが常に同じString.IndexOfString.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-11T21:33:07.140 に答える
1

これはあなたが望むことをするはずです:

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-11T22:05:58.100 に答える
0

Also, as mellamokb mentioned, if you're running this application on the PC you're querying, you can use

System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces

to get at this information.

于 2012-07-11T22:35:48.147 に答える