在Visual Basic中迭代文本文件

时间:2011-07-25 17:38:18

标签: vb.net arrays visual-studio-2010

好的,所以我正在创建一个程序,它将从文本文件中读取,将每一行转换为数组,即。每个刻度线发送每条线1。这是2010 Visual Basic。 我最近得到的就是一次性发送,我在夜间工作并慢慢摧毁它。 理想情况下,我希望按钮1单击以从LocationTB中的文件填充数组,然后启动计时器。计时器应该在GapTB间隔上一次发送一行。

Public Class Form1
Public TextLine As String
Public MyFileName As String
Public MyNewLine(1000) As String
Private Property z As Integer
Private Property objReader As Object


Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    If RadioButton2.Checked = True Then
        Dim Textline As String = ""
        Dim FILE_NAME As String = LocationTB.Text
        Dim objReader As New System.IO.StreamReader(FILE_NAME)
        MyFileName = LocationTB.Text
        FileOpen(1, MyFileName, OpenMode.Input, OpenAccess.Read, OpenShare.Shared)
        z = 0

        Do Until EOF(1)
            MyNewLine(z) = LineInput(1)
            z = z + 1 
        Loop

        FileClose(1)
    End If

    Timer1.Interval = GapTB.Text
    Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    If RadioButton1.Checked = True Then
        AppActivate(Hook.Text)
        SendKeys.Send(SimpleTB.Text)
        SendKeys.Send((Chr(13)))

    ElseIf RadioButton2.Checked = True Then
        For Each 
            TextLine = TextLine & objReader.ReadLine
            AppActivate(Hook.Text)
            SendKeys.Send(TextLine)
            SendKeys.Send((Chr(13)))
        Next

    Else

            MsgBox("File Does Not Exist")

    End If


End Sub

2 个答案:

答案 0 :(得分:2)

这是你要找的东西吗? 它会将文件的内容(在本例中为“C:\ mark.txt”)写入Visual Studio的输出窗口。

Public Class Form1
    Private myTimer As Timer

    Private lines As String()
    Private currentLine As Integer

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        lines = IO.File.ReadAllLines("C:\mark.txt")
        currentLine = 0

        myTimer = New Timer()

        AddHandler myTimer.Tick, AddressOf myTimer_Tick

        myTimer.Interval = 1000
        myTimer.Enabled = True
    End Sub

    Private Sub myTimer_Tick(sender As Object, e As EventArgs)
        If currentLine < lines.Count Then
            Dim lineToSend As String = lines(currentLine)
            Debug.Print(lineToSend)
            currentLine += 1
        Else
            myTimer.Enabled = False
        End If
    End Sub
End Class

<强>买者 上面的代码不可扩展。如果您 SURE ,该文件将始终很小,那么它会发生(也就是说,它们从不总是很小)。

要使这个可扩展,您需要保持文件打开,并根据需要读取每一行,而不是一次加载整个文件内容。

答案 1 :(得分:0)

我知道如何对我自己开始的应用程序执行此操作:以下是使计算器工作的代码:

   Dim ProcID As Integer
    ' Start the Calculator application, and store the process id.
    ProcID = Shell("CALC.EXE", AppWinStyle.NormalFocus)
    ' Activate the Calculator application.
    AppActivate(ProcID)
    ' Send the keystrokes to the Calculator application.
    My.Computer.Keyboard.SendKeys("22", True)
    My.Computer.Keyboard.SendKeys("*", True)
    My.Computer.Keyboard.SendKeys("44", True)
    My.Computer.Keyboard.SendKeys("=", True)
    ' The result is 22 * 44 = 968.

AppActivate应该知道要激活的表单的确切标题。如果不知道,请迭代进程以找到您的进程。如果标题被正确指定,它应该起作用