如何将批处理的输出重定向到VB 2010 Express中的textbox1? [英] How can I re-direct the output of a batch to the textbox1 in VB 2010 Express?

查看:93
本文介绍了如何将批处理的输出重定向到VB 2010 Express中的textbox1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经研究了这个约3天了,我发现的一切都不起作用或引发10+错误。我正在尝试从批处理文件(显示生成的字符串的CMD窗口)中获取输出并将其插入到我的VB Form1上的TextBox1中。因此,当我按下按钮时,批处理会打开(在工具栏上)一个CMD窗口,显示字符串,因为它们在插入可能是打开的文件对话框时生成,但现在是文本框。(多行)

有没有办法做到这一点和/或这是创建管道进来的地方?我提前道歉我是一个一周大的初学者。以下是我对button1的看法。







So I have researched this for about 3 days now and everything that I find doesn't work or throws 10+ Errors. I am trying take the output from a batch file (CMD Window that shows generated strings) and insert it into TextBox1 on my VB Form1. So when I push the button, the batch is executed which opens up (on toolbar) a CMD window, shows the strings as they are produced while being inserted into what might be an open file dialog but for now its textbox1.(multiline)
Is there a way to do this and/or is this where create pipeline comes in? I apologize in advance I am a week old beginner. Below is what I have for button1.



Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Shell("C:\Test.bat")

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim myprocess As New Process
        Dim StartInfo As New System.Diagnostics.ProcessStartInfo
        StartInfo.FileName = "cmd" 'starts cmd window
        StartInfo.RedirectStandardInput = True
        StartInfo.RedirectStandardOutput = True
        StartInfo.UseShellExecute = False 'required to redirect
        StartInfo.CreateNoWindow = True '<---- creates no window, obviously
        myprocess.StartInfo = StartInfo
        myprocess.Start()
        Dim SR As System.IO.StreamReader = myprocess.StandardOutput
        Dim SW As System.IO.StreamWriter = myprocess.StandardInput
        SW.WriteLine(TextBox1) 'the command you wish to run.....
        SW.WriteLine("exit") 'exits command prompt window
        txtResults.Text = SR.ReadToEnd 'returns results of the command window
        SW.Close()
        SR.Close()
    End Sub
End Class

推荐答案

如果TextBox1包含要执行的命令,为什么不简单地将其作为StartInfo.Arguments传递?

http://msdn.microsoft .com / zh-cn / library / system.diagnostics.processstartinfo.arguments(v = vs.110).aspx [ ^ ]



看看这个;那里可能有一些有用的东西:

ProcessCommunicator [ ^ ]





此外,您不需要创建新的StartInfo。
If TextBox1 contains the command to execute, why not simply pass it as StartInfo.Arguments?
http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.arguments(v=vs.110).aspx[^]

Have a look at this; there might be something useful in there:
ProcessCommunicator[^]


Also, you don't need to create a new StartInfo.


这篇关于如何将批处理的输出重定向到VB 2010 Express中的textbox1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆