2011-10-19 155 views
0

所以我觉得像即时通讯相当接近的第一个字,但我也有一种感觉,我是混合起来的StreamReader和ReadAllLines填充组合框与文本文件

........... .................................................. .......................

选项严格在

进口System.IO

公共类Form4

Dim file As System.IO.StreamWriter 

Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 

    file = My.Computer.FileSystem.OpenTextFileWriter("c:\devices.bat", False) 
    file.WriteLine("@echo off") 
    file.WriteLine("cd " & Form1.TextBox2.Text) 
    file.WriteLine("adb devices > C:\devices.txt") 
    file.Close() 
    Shell("C:\devices.bat", AppWinStyle.Hide, True, 500) 

    Dim output() = System.IO.File.ReadAllLines("C:\deviceinfo2.txt") 
    Dim Devices As String = "" 
    Dim line() As String = {} 

    For X = 1 To output.Count = -1 
     line = output(X).Split(New Char() {("  ")}) 
     Devices = line(0) 
     ComboBox1.Items.Add(Devices) 

    Next 

    output.Close() 
    output.Dispose() 

End Sub 

结束等级

......................................... ...............................

我想要做的是从第二行开始阅读的devices.txt文件,然后读取每行的第一个单词,直到文本文件完成。

这看起来很简单,但就像我说的,我觉得我是混合的StreamReader与readalllines

任何帮助表示赞赏

回答

1
Class Test 
    Public Sub Main() 
     Try 
      ' Create an instance of StreamReader to read from a file. 
      ' The using statement also closes the StreamReader. 
      Using sr As New StreamReader("TestFile.txt") 
       Dim line, firstWord As String 
       Dim i as Integer = 0 
       ' Read and display lines from the file until the end of 
       ' the file is reached. 
       Do 
        line = sr.ReadLine() 
        If Not (line Is Nothing) AndAlso i > 0 Then 
         firstWord = line.Split(" ")(i) 
         'do your logic 
        End If 
        i += 1 
       Loop Until line Is Nothing 
      End Using 
     Catch e As Exception 
      ' Let the user know what went wrong. 
     End Try 
    End Sub 
End Class 

从MSDN抓起这一点,并修改它。它应该编译,但我没有测试它。这将循环遍历行,1乘1,跳过第一行并获取每行后的第一个单词。希望这可以帮助。

+0

我在假设“第一个单词”意味着它后面跟着一个空格并且它的第一个字符是该行上的第一个字符的情况下操作。如果情况并非如此,分割将不得不被改变,否则你将不得不解析字符串,无论开始“第一个单词”,然后结束。 – Yatrix

+0

它开始在第二行,但它需要从第二行的第二个字,并找不到第3行和第4行 – Nefariis

+0

我想通了:firstWord = line.Split(CChar(“\t”))(0)FTW – Nefariis