在TextBox中打印FTP输出

时间:2016-03-10 06:58:07

标签: vb.net

我有下面的.net vb代码,遗憾的是它没有按预期工作。

1)退出时我无法关闭txtConsoleOut(文本框)(发生异常错误)

2)哈希#####(来自ftp代码)不会立即出现在名为 txtConsoleOut 的TextBox中,它只会在进程(FTP)完成后出现。

下面的代码只需要一个名为btnRun的按钮和一个名为TxtConsoleOut的文本框

Imports System.Diagnostics

Public Class form1
    Private psi As ProcessStartInfo
    Private cmd As Process

    Private Delegate Sub InvokeWithString(ByVal text As String)

    Private Sub CMD_Exited(ByVal sender As Object, ByVal e As EventArgs)
         txtConsoleout.hide()
    End Sub
    ' This sub gets called in a different thread so invokation is required
    Private Sub Async_Data_Received(ByVal sender As Object, ByVal e As DataReceivedEventArgs)
        Me.Invoke(New InvokeWithString(AddressOf Sync_Output), e.Data)
    End Sub
    Private Sub Sync_Output(ByVal text As String)
        txtConsoleOut.AppendText(text & Environment.NewLine)
        txtConsoleOut.ScrollToCaret()
    End Sub

    Private Sub btnRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRun.Click
        btnRun.Text = "Starting!!!"
        psi = New ProcessStartInfo("ftp", " -s:c:\compu\ftp-p.txt")

        Dim systemencoding As System.Text.Encoding =
            System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage)
        With psi
            .UseShellExecute = False  ' Required for redirection
            .RedirectStandardError = True
            .RedirectStandardOutput = True
            .RedirectStandardInput = True
            .CreateNoWindow = True
            .StandardOutputEncoding = systemencoding  ' Use OEM encoding for console applications
            .StandardErrorEncoding = systemencoding
        End With
        ' EnableraisingEvents is required for Exited event
        cmd = New Process With {.StartInfo = psi, .EnableRaisingEvents = True}
        AddHandler cmd.ErrorDataReceived, AddressOf Async_Data_Received
        AddHandler cmd.OutputDataReceived, AddressOf Async_Data_Received
        AddHandler cmd.Exited, AddressOf CMD_Exited

        cmd.Start()

        ' Start async reading of the redirected streams
        ' Without these calls the events won't fire
        cmd.BeginOutputReadLine()
        cmd.BeginErrorReadLine()
    End Sub
    'Option Explicit
    'Private  Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
End Class

0 个答案:

没有答案