次の方法で共有


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

Comments

  • Anonymous
    April 01, 2006
    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.

  • Anonymous
    April 02, 2006
    Sounds good!! Can you share the code please?? :o)

  • Anonymous
    August 03, 2006
    Thanks Rahul, its really very helpful for me.

  • Anonymous
    September 06, 2006
    Very helpful, concise article! Thumbs way up!!

  • Anonymous
    January 26, 2007
    Great code! Needed a little tweaking for C#, but very useful! Thanks!

  • Anonymous
    April 19, 2007
    top