vb.net:如何从cmd获取结果到文本框

时间:2017-03-14 09:41:53

标签: vb.net shell cmd textbox command

我尝试在cmd中运行此命令并将结果发送到我的textbox4.text但没有成功 命令:cscript“%windir%\ system32 \ slmgr.vbs”/ xpr | findstr / S / M / I / C:“永久”

导致需要进入我的textbox4: 机器永久激活。

我写这段代码没有成功:

    Dim qassam As String

    qassam = Shell("whoami")
    TextBox4.Text = CStr(qassam)

并且我发现了这个但没有工作:

 Dim oProcess As New Process()
    Dim oStartInfo As New ProcessStartInfo("cmd.exe", "cscript %windir%\system32\slmgr.vbs /xpr | findstr /S /M /I /C:permanently ")
    oStartInfo.UseShellExecute = False
    oStartInfo.RedirectStandardOutput = True
    oProcess.StartInfo = oStartInfo
    oProcess.Start()

    Dim sOutput As String
    Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
        sOutput = oStreamReader.ReadToEnd()
    End Using

    TextBox4.Text = sOutput   'txtOutput being the output textbox.

我是vb.net的新手我需要简单的代码才能理解

1 个答案:

答案 0 :(得分:0)

通过添加此/ k来解决 (“cmd.exe”,“/ c cscript%windir%\ system32 \ slmgr.vbs / xpr | findstr / S / M / I / C:永久”)`

 Dim oProcess As New Process()
    Dim oStartInfo As New ProcessStartInfo("cmd.exe", " /c cscript ""%windir%\system32\slmgr.vbs"" /xpr | findstr ""The machine""")
    oStartInfo.UseShellExecute = False
    oStartInfo.RedirectStandardOutput = True
    oProcess.StartInfo = oStartInfo
    oProcess.Start()

    Dim sOutput As String
    Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
        sOutput = oStreamReader.ReadToEnd()
    End Using

    TextBox4.Text = sOutput