Rahul Soni's blog

Never assume the obvious is true!

Run a command from the command prompt and get its output in a Windows Application

Run a command from the command prompt and get its output in a Windows Application

  • Comments 6

In this pick, I wanted to show you how to execute commands on the command prompt and get its output in a regular Windows Application. The command in the following code will be entered via a VB.NET Windows application and we will get the output in a Textbox called txtOutput.

The command I have used is actually listing out all the websites (virtual folders which are not marked as application are not listed) on your IIS, using ADSUtil.vbs utility. Copy the following code block in any of the methods and you should be good to go (provided you have IIS installed in the default location on your box).

So, here is the requirement for me. Run the following command and get me the output in VB.NET Texbox. Yep, it's that simple!!!

cscript C:\Inetpub\AdminScripts\adsutil.vbs find AppRoot

            Try
                Dim prcProcess As New Process()
                Dim srdOutput As IO.StreamReader
                prcProcess.StartInfo.FileName = "cmd.exe"
                prcProcess.StartInfo.UseShellExecute = False           
                prcProcess.StartInfo.CreateNoWindow = True
                prcProcess.StartInfo.RedirectStandardOutput = True
                prcProcess.StartInfo.RedirectStandardInput = True
                prcProcess.StartInfo.RedirectStandardError = True
                prcProcess.Start()
                Dim swrInput As IO.StreamWriter = prcProcess.StandardInput
                swrInput.AutoFlush = True
                swrInput.Write("cscript C:\Inetpub\AdminScripts\adsutil.vbs find AppRoot > C:\Temp\AdplusOutput.txt" & System.Environment.NewLine)
                swrInput.Write("exit" & System.Environment.NewLine)
                prcProcess.WaitForExit()
                If Not prcProcess.HasExited Then
                    prcProcess.Kill()
                End If
                swrInput.Flush()
                srdOutput = New IO.StreamReader("C:\Temp\AdPlusOutput.txt")
                txtOutput.Text = srdOutput.ReadToEnd
                srdOutput.Close()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

Hope that helps!
-Rahul Soni

Attachment: RahulSoni.png
  • Very cool.

    I did it slightly differently by inheriting from Form and passing stdout to the new class. That way you can integrate ex-console type code to forms or at least get an easier view of what is cracking in the console code.
  • Sounds good!! Can you share the code please?? :o)
  • Thanks Rahul, its really very helpful for me.

  • Very helpful, concise article! Thumbs way up!!
  • Great code! Needed a little tweaking for C#, but very useful! Thanks!

  • top

Page 1 of 1 (6 items)
Leave a Comment
  • Please add 1 and 3 and type the answer here:
  • Post