简体   繁体   English

vb.net遍历并从文件中提取信息

[英]vb.net loop through and extract info from file

I have a file where part of it looks like this: 我有一个文件,其中一部分看起来像这样:

 bank: _nameless.292E.6438 
 player: _nameless.2843.0C10
 companies: 312

This is my vb.net file that is meant to loop through and give me the line that starts with bank: 这是我的vb.net文件,旨在遍历并给我以bank开头的行:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Using sr As New StreamReader(currentfile)
        Dim line As String
        line = sr.ReadToEnd()
        If line.StartsWith(" bank:") Then
            Console.WriteLine(line)
        End If
    End Using
End Sub

However, for some reason, this is not working and doesn't write anything to the console. 但是,由于某种原因,这是行不通的,并且不会向控制台写入任何内容。 I have tried it without the if statement and it writes the whole file as expected. 我尝试了没有if语句的情况,并且按预期方式写入了整个文件。

The bank: was copied and pasted straight from the text file so this is definately the correct text. bank:直接从文本文件复制并粘贴,因此绝对是正确的文本。

Thanks in advance, Marcus 预先感谢,马库斯

Read each line like this: 像这样阅读每一行:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  Using sr As New StreamReader(currentfile)
    While Not sr.EndOfStream()
      Dim line As String = sr.ReadLine
      If line.StartsWith(" bank:") Then
        Console.WriteLine(line)
      End If
    End While
  End Using
End Sub

You may skip off Blank space like this, 您可以跳过这样的空格,

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Using sr As New StreamReader(currentfile)
 While Not sr.EndOfStream()
   Dim line As String = sr.ReadLine
   line = line.Trim()    'This will remove any white space before and after in the line
   If line.StartsWith("bank:") Then
     Console.WriteLine(line)
   End If
 End While
End Using
End Sub

use this 用这个

If line.StartsWith("bank:") Then Console.WriteLine(line) End If 如果line.StartsWith(“ bank:”)然后Console.WriteLine(line)结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM