0

I recently building codes for cmd commands by vb.net. It cannot work by just popping up the same form. I hope that someone can fix it.

Imports System.IO
Public Class Form1
    Private Sub Execute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Execute.Click
        Result.Text = RunCmd(Command.Text)
    End Sub

    Private Function RunCmd(ByVal cmd As String) As String
        Dim p As Process = New Process
        With p.StartInfo
            .FileName = "cmd.exe"
            .Arguments = "/c " + cmd
            .UseShellExecute = False
            .RedirectStandardInput = True
            .RedirectStandardOutput = True
            .RedirectStandardError = True
            .CreateNoWindow = True
        End With
        p.Start()
        'p.StandardInput.WriteLine(command);
        'p.StandardInput.WriteLine("exit");
        Dim reader As StreamReader = p.StandardOutput
        Dim output As String = reader.ReadToEnd()
        Return output
    End Function
End Class

I expect that I can run the command in the textbox. But it can't.

MatSnow
  • 7,357
  • 3
  • 19
  • 31
Matthew
  • 1
  • 1
  • What are the issues you are facing with this code and what is the outcome? – Azaz ul Haq Aug 20 '19 at 08:00
  • The form just keep poping up again and again when I click "execute" button. – Matthew Aug 20 '19 at 08:05
  • I'm not sure which form you are talking about? There is one form that has Execute button on it and that form will remain there unless you close it. Try debug the RunCmd method and see how it executes. – Azaz ul Haq Aug 20 '19 at 08:08
  • Oh! My code is work in VB 2010 Express but not in visual studio 2019!!! – Matthew Aug 20 '19 at 08:14
  • What is the error in VS 2019? – Azaz ul Haq Aug 20 '19 at 08:18
  • Possible duplicate of [How to run DOS/CMD/Command Prompt commands from VB.NET?](https://stackoverflow.com/questions/10261521/how-to-run-dos-cmd-command-prompt-commands-from-vb-net) – Azaz ul Haq Aug 20 '19 at 08:19
  • What are you passing in `Command.Text` that make a Form pop up? Which Form are you referring to? The current `Form1`? Note that this procedure is synchronous, `Form1` will freeze until the command is completed. – Jimi Aug 20 '19 at 08:44
  • I just ran the code and didn't get any popup. It's behaving as expected for me, runs the command and returns the results. Is it too quick for a screenshot? or are you getting any error/exception? – Fabulous Aug 20 '19 at 12:12

0 Answers0