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

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

RahulSoni.png