2011-10-19 208 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