微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从cmd外壳读取VB.NET中的cmd输出?

如何解决如何从cmd外壳读取VB.NET中的cmd输出?

| 我正在使用gnokii发送SMS。 我的VB代码
Dim xCmd As String
xCmd = \"cmd.exe /c echo msgcontent \"| c:\\gnokii\\gnokii.exe --sendsms 12345678\"
Shell(xCmd)
注意事项: 我确实尝试将输出重定向到.txt文件,但.txt文件似乎为空。此外,该程序可能必须每秒发送多个SMS,因此创建.txt是不可行的。 Process.Start()不可行,因为我必须检查gnokii.exe是否正在运行。 我需要输出来检查SMS是否成功发送。 我尝试使用(下面的代码),但是也没有用;没有显示输出函数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
结束功能     

解决方法

        尝试这个:
    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
    ,        要将输出发送到.txt文件,(我能找到的最佳解决方案) 更换
xCmd = \"cmd.exe /c echo msgcontent \"| c:\\gnokii\\gnokii.exe --sendsms 12345678 > file.txt\"
xCmd = \"cmd.exe /c echo msgcontent \"| c:\\gnokii\\gnokii.exe --sendsms 12345678 2> file.txt\"
    ,        您可以使用此100%的作品,但只会显示结果 如何在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 & \"\\123.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 & \"\\123.text\") = False Then
    IO.File.Create(Application.StartupPath & \"\\123.text\")
End If
\'-------------------------------------------------------------------------
该代码也可以从以下链接获得:http://pastebin.com/iEhv61jG     ,        我自己可能会建议这样的事情。我认为这类似于其他人发布的内容,但是它提供了更多功能。
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
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。