如何将输出发送到日志文件和richtextbox

时间:2019-09-03 18:39:07

标签: vb.net winforms output

我有以下代码可用于将输出发送到Richtextbox。我也想不出如何选择将输出也发送到日志文件。这是我选择记录日志的代码的开头。我无法弄清楚如何将其记录到文件位置。

SELECT
    *

FROM
    chat

WHERE
    idreceiver = 2
    AND
    sentTime = (SELECT MAX(sentTime) FROM chat WHERE idreceiver = 2)

这是我的工作代码,用于在Richtextbox中显示输出。

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If (OpenFileDialog1.ShowDialog() = DialogResult.OK) Then
        TextBox31.Text = Chr(34) & OpenFileDialog1.FileName & Chr(34)
    End If
    End Sub

注意:这就是我以前能够记录输出的内容

    Private Sub ExecuteButton_Click(sender As Object, e As EventArgs) Handles ExecuteButton.Click
    System.Windows.Forms.Application.DoEvents()
    Dim myprocess As New Process
    Dim startinfo As New ProcessStartInfo(TextBox3.Text, TextBox1.Text) With {
        .UseShellExecute = False,
        .RedirectStandardOutput = True,
        .CreateNoWindow = True
    }
    myprocess.StartInfo = startinfo
    myprocess.Start()
    Dim str1 As String = ""
    Using MyStreamReader As IO.StreamReader = myprocess.StandardOutput
        str1 &= MyStreamReader.ReadToEnd
    End Using
    RichTextBox1.Text = str1
End Sub

1 个答案:

答案 0 :(得分:0)

尝试以下示例:

    Dim MyPath = "C:\MyLog.Log"
    FileOpen(1, MyPath, OpenMode.Append)
    Dim lNumberofRecs = LOF(1)
    Print(1, Format(Now, "dd-MMM-yyyy HH:mm:ss") & "#Import Begin" & vbCrLf)
    FileClose(1)

或与此:

    Dim objWriter As New System.IO.StreamWriter("C:\MyData\mylog.log", True)
    objWriter.WriteLine("each log data you have")
    objWriter.Close()