2012-07-11 26 views
2

嗨使用cmd將用戶網絡規範打印到使用ipconfig> computer.txt的文本文件中。這方面的一個例子是使用vb.net從文本文件中挑選信息

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

+1

有更低層次的獲取該信息的方法,這將更加可靠和高效。點擊這裏查看很多好的想法和鏈接:http://www.msvisual.com/Installing-RAS-programmatically-t25743.html。 – mellamokb 2012-07-11 21:31:44

回答

1

如果格式總是相同String.IndexOfString.Substring找到子網掩碼/默認網關的最佳方式是effcient方式:

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

+0

你會在特定的行之後說些什麼?謝謝 – user1244772 2012-07-11 21:40:56

+0

指定_particular line_。在n行之後或包含特定文本的行之後的行? – 2012-07-11 21:42:16

+0

@ user1244772:既然你提到過「_x lines down_」,我假設你想跳過x行。相應地編輯我的答案。 – 2012-07-11 21:54:03

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 
0

而且,mellamokb提到的,如果你正在運行此應用程序在您查詢的PC上,您可以使用

System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces 

獲取此信息。

相關問題