如何从 cmd shell 读取 VB.NET 中的 cmd 输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6397435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to I read cmd output in VB.NET from cmd shell?
提问by Joyce
I am using gnokii to send out SMSes.
我正在使用 gnokii 发送短信。
My VB Codes:
我的VB代码:
Dim xCmd As String
xCmd = "cmd.exe /c echo msgcontent "| c:\gnokii\gnokii.exe --sendsms 12345678"
Shell(xCmd)
Points to note:
注意事项:
I did try to redirect the output to a .txt file but the .txt file appears to be empty. Besides, the program may have to send out multiple SMSes every second, so creating a .txt is not feasible.
Process.Start() is not feasible because I have to check if gnokii.exe is running.
I need the output to check if the SMS is sent successfully.
I tried using (codes below), but it didn't work either; no output was shown.
Function exe(ByVal fileName, ByVal args)
Dim p As Process = New Process Dim output As String With p .StartInfo.CreateNoWindow = True .StartInfo.UseShellExecute = False .StartInfo.RedirectStandardOutput = True .StartInfo.FileName = fileName .StartInfo.Arguments = args .Start() output = .StandardOutput.ReadToEnd End With Return output
End Function
我确实尝试将输出重定向到 .txt 文件,但 .txt 文件似乎为空。此外,程序每秒可能要发送多条短信,因此创建一个.txt 文件是不可行的。
Process.Start() 不可行,因为我必须检查 gnokii.exe 是否正在运行。
我需要输出来检查短信是否成功发送。
我尝试使用(下面的代码),但它也不起作用;没有显示输出。
函数 exe(ByVal fileName, ByVal args)
Dim p As Process = New Process Dim output As String With p .StartInfo.CreateNoWindow = True .StartInfo.UseShellExecute = False .StartInfo.RedirectStandardOutput = True .StartInfo.FileName = fileName .StartInfo.Arguments = args .Start() output = .StandardOutput.ReadToEnd End With Return output
结束函数
采纳答案by Joyce
To send output to a .txt file, (the best solution I can find)
将输出发送到 .txt 文件,(我能找到的最佳解决方案)
REPLACE
代替
xCmd = "cmd.exe /c echo msgcontent "| c:\gnokii\gnokii.exe --sendsms 12345678 > file.txt"
WITH
和
xCmd = "cmd.exe /c echo msgcontent "| c:\gnokii\gnokii.exe --sendsms 12345678 2> file.txt"
回答by aligray
Try this:
尝试这个:
Dim p As Process = New Process
Dim output As String
With p
.StartInfo.CreateNoWindow = True
.StartInfo.RedirectStandardOutput = True
.StartInfo.UseShellExecute = False
.StartInfo.FileName = fileName
.StartInfo.Arguments = args
.Start()
output = .StandardOutput.ReadToEnd
.WaitForExit()
End With
Return output
回答by Wisdom oparaocha
You can use this 100% works but it will only show you the results
你可以使用这个 100% 的作品,但它只会向你显示结果
How to show shell results in vb.net:
如何在 vb.net 中显示 shell 结果:
'create 1 textbox1
'create 1 button1
'create 1 richtextbox1
'in the start up directory of this program make a file could 123.text
'------------------------------------------------------------------------
Dim read As System.IO.StreamReader
read = File.OpenText(Application.StartupPath & "3.text")
Shell("cmd.exe /c" & TextBox1.Text + ">123.text")
Do Until read.EndOfStream
RichTextBox1.Text = read.ReadLine & vbCrLf
Loop
'--------------------------------------------------------------------------
'you can add on the top to create the file if it does not exists,
If IO.File.Exists(Application.StartupPath & "3.text") = False Then
IO.File.Create(Application.StartupPath & "3.text")
End If
'-------------------------------------------------------------------------
The code is also available at this link http://pastebin.com/iEhv61jG
该代码也可在此链接http://pastebin.com/iEhv61jG
回答by Skotte
I might suggest something like this, myself. This is similar to what someone else posted, but it offers a little more functionality, I think.
我自己可能会建议这样的事情。这类似于其他人发布的内容,但我认为它提供了更多功能。
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Shell("cmd.exe /c " & TextBox1.Text + " > c:\temp\output.txt")
Dim read As System.IO.StreamReader
read = File.OpenText("c:\temp\output.txt")
RichTextBox1.Clear()
Do Until read.EndOfStream
RichTextBox1.Text += read.ReadLine & vbCrLf
Loop
RichTextBox1.Select(RichTextBox1.Text.Length, 0)
RichTextBox1.ScrollToCaret()
End Sub
End Class