在列表框中显示命令提示符中显示的消息

发布于 2024-11-28 11:08:47 字数 688 浏览 3 评论 0原文

实际上,我想显示 cmd 提示符中显示的消息,例如:

Ping google.com -t

以下消息将显示在 cmd 提示符中:

Reply from 74.125.235.17: bytes=32 time=133ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51

Ping statistics for 74.125.235.17:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 130ms, Maximum = 133ms, Average = 130ms

我想将确切的信息显示到我的程序的列表框在命令提示符中显示时立即显示,而不是在整个过程完成后。我怎样才能这样做?有什么帮助吗?我正在使用 C#/vb.net 。

就像在 ping google.com -t 中一样,我想在列表框中立即显示每条回复消息。

Actually I want to display the messages shown in cmd prompt like, if i do:

Ping google.com -t

The following message will be displayed in the cmd prompt:

Reply from 74.125.235.17: bytes=32 time=133ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51
Reply from 74.125.235.17: bytes=32 time=130ms TTL=51

Ping statistics for 74.125.235.17:
Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
Minimum = 130ms, Maximum = 133ms, Average = 130ms

I want to display the exact information into the list box of my program instantly when it is displayed in the command prompt not after whole process is completed. How can i do so? Any help? I am using C#/vb.net .

As in the ping google.com -t, I want to display the each reply message instantly in the list box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

金橙橙 2024-12-05 11:08:47

为此,您需要利用标准输出上的异步读取...另请参阅...

在这里您可以找到所描述问题的解决方案以及完整的源代码。 ..它甚至考虑了 stderr...

其他有趣的资源:

For this you need to utilize async read on standardoutput... see also this...

Here you can find a solution for the described problem complete with source code... it even takes stderr into account...

Other interesting resources:

书信已泛黄 2024-12-05 11:08:47

试试这个:

Private Results As String

'The "Delegate" is used to correct the threading issue (Can't update control directly in VB.net 08/10), and invokes the needed text update.
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)

Private Sub UpdateText()
resultsTextBox.Text = Results
End Sub

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

Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub

Private Sub CMDAutomate()

Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo

'Starts the CMD Prompt
StartInfo.FileName = "cmd.exe"
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True

'Required to redirect
StartInfo.UseShellExecute = False

'Disables the creation of a CMD Prompt outside application.
StartInfo.CreateNoWindow = True


myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput

'Runs the command you entered...
SW.WriteLine(TextBox1.Text)

'Exits CMD Prompt 
SW.WriteLine("exit")

'Displayes the results...
Results = SR.ReadToEnd
SW.Close()
SR.Close()

'Invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub

Try this:

Private Results As String

'The "Delegate" is used to correct the threading issue (Can't update control directly in VB.net 08/10), and invokes the needed text update.
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)

Private Sub UpdateText()
resultsTextBox.Text = Results
End Sub

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

Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub

Private Sub CMDAutomate()

Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo

'Starts the CMD Prompt
StartInfo.FileName = "cmd.exe"
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True

'Required to redirect
StartInfo.UseShellExecute = False

'Disables the creation of a CMD Prompt outside application.
StartInfo.CreateNoWindow = True


myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput

'Runs the command you entered...
SW.WriteLine(TextBox1.Text)

'Exits CMD Prompt 
SW.WriteLine("exit")

'Displayes the results...
Results = SR.ReadToEnd
SW.Close()
SR.Close()

'Invokes Finished delegate, which updates textbox with the results text
Invoke(Finished)
End Sub
梦里°也失望 2024-12-05 11:08:47

为您快速解决。

Quick solution for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文