首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用vb.net从文本文件中提取信息

使用vb.net从文本文件中提取信息
EN

Stack Overflow用户
提问于 2012-07-12 05:25:55
回答 3查看 341关注 0票数 2

我正在使用cmd使用ipconfig > computer.txt将用户网络规范打印到一个文本文件中。这方面的一个例子是

代码语言:javascript
复制
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查找子网掩码/默认网关的最佳方法是什么

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-07-12 05:33:07

如果格式始终相同,则String.IndexOfString.Substring是有效的方法:

代码语言:javascript
复制
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。

票数 1
EN

Stack Overflow用户

发布于 2012-07-12 06:05:58

这应该是您想要的结果:

代码语言:javascript
复制
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
票数 1
EN

Stack Overflow用户

发布于 2012-07-12 06:35:48

此外,正如mellamokb所提到的,如果您在要查询的PC上运行此应用程序,则可以使用

代码语言:javascript
复制
System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces

来获取这些信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11441743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档