使用文本文件的第一个单词填充组合框

时间:2011-10-19 20:07:34

标签: vb.net visual-studio visual-studio-2010 file-io

所以我觉得我非常接近,但我也有一种感觉,我混淆了StreamReader和ReadAllLines

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

Option Strict On

Imports System.IO

Public Class 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 个答案:

答案 0 :(得分: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获取并修改它。它应该编译,但我没有测试它。这将逐行循环,跳过第一行,然后抓住每一行的第一个单词。希望这可以帮助。