Help with ProcessStartInfo - RedirectStandardInput & RedirectStandardOutput

Hi,

I have a command prompt program which has stdOut and strIn.  How can I capture this information using the .NET StreamWriter and StreamReader.  I think I need to use the RedirectStandardInput and RedirectStandardOutput from the ProcessStartInfo object to somehow do this.

Any help would be appreciated!

Thanks
LVL 1
narmi2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sr101880Commented:
You need to set:

        StartInfo.RedirectStandardInput = True
        StartInfo.UseShellExecute = False

before you can use StandardInput.Write method.  I think that it is the same for the output.  Let me know if you need more info on launching your program as a process.
0
narmi2Author Commented:
This is what I have so far:

------------------------------------------------
    Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        Dim _Process As Process = New Process
        Dim s As String

        _Process.StartInfo.FileName = "cmd.exe"
        _Process.StartInfo.UseShellExecute = False
        _Process.StartInfo.CreateNoWindow = True
        _Process.StartInfo.RedirectStandardInput = True
        _Process.StartInfo.RedirectStandardOutput = True
        _Process.StartInfo.RedirectStandardError = True
        _Process.Start()

        Dim sIn As StreamWriter = _Process.StandardInput
        sIn.AutoFlush = True

        Dim sOut As StreamReader = _Process.StandardOutput
        Dim sErr As StreamReader = _Process.StandardError

        sIn.Write("dir c:\*.txt" & System.Environment.NewLine)
        sIn.Write("exit" & System.Environment.NewLine)
        s = sOut.ReadToEnd()

        If Not _Process.HasExited Then
            _Process.Kill()
        End If

        sIn.Close()
        sOut.Close()
        sErr.Close()
        _Process.Close()

        MessageBox.Show(s)
    End Sub
------------------------------------------------

What this does is runs cmd.exe and StandardInputs "dir c:\*.txt" and displays everything which should have been displayed in the normal command prompt in a messagebox instead.

How do I get this text in a listbox instead of the messagebox which I am currently using?

Thanks
0
sr101880Commented:
This is a less complicated way of getting your info into a list box:

Imports System.IO '<---  Make sure this is above your class

Dim pattern As String = "*.txt"
        If pattern.IndexOf("(") >= 0 Then
            pattern = pattern.Substring(0, pattern.IndexOf("("))
        End If

        ' Get the files.
        lstfiles.Items.Clear()
        Dim dir_info As New DirectoryInfo("C:\")
        Dim file_infos() As FileInfo
        file_infos = dir_info.GetFiles(pattern)
        For Each file_info As FileInfo In file_infos
            lstfiles.Items.Add(file_info.Name)  '<--- Change lstfiles to your listbox name
        Next file_info

Let me know if you have any questions.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

narmi2Author Commented:
Thanks for the suggestion, but that was only an example I posted, I would like to get the output of another command prompt program and into my program.
0
sr101880Commented:
sorry, I was just trying to keep things simple.  I will get back to you shortly with some more info. :-)
0
sr101880Commented:
I got part of this example from the code project.  I think it is more in line with the solution you had in mind.  You can see that this loads each line as an item.  Part of this is commented out, in that section you can setup a custom string selection by moving the listbox to that section and using the examples.  Let me know if you have any questions.

    Dim _Process As Process = New Process
        Dim s As String

        _Process.StartInfo.FileName = "cmd.exe"
        _Process.StartInfo.UseShellExecute = False
        _Process.StartInfo.CreateNoWindow = True
        _Process.StartInfo.RedirectStandardInput = True
        _Process.StartInfo.RedirectStandardOutput = True
        _Process.StartInfo.RedirectStandardError = True
        _Process.Start()

        Dim sIn As StreamWriter = _Process.StandardInput
        sIn.AutoFlush = True

        'Dim sOut As StreamReader = _Process.StandardOutput
        Dim sErr As StreamReader = _Process.StandardError

        sIn.Write("dir c:\*.txt" & System.Environment.NewLine)
        sIn.Write("exit" & System.Environment.NewLine)


        Dim tmpstr2 As String = String.Empty

        Do
            tmpstr2 = _Process.StandardOutput.ReadLine
            '' Add some text to the file.
            If Not (tmpstr2 Is Nothing) Then
                '    cnt += 1
                '    'The output is fixed length
                '    If cnt > 5 Then
                '        
                '        values(0) = tmpstr2.Substring(0, 15).Trim 'Host name
                '        values(1) = tmpstr2.Substring(16, 8).Trim 'ID
                '        values(2) = tmpstr2.Substring(25, 20).Trim 'accessed by
                '        values(3) = tmpstr2.Substring(46, 10).Trim 'type
                '        values(4) = tmpstr2.Substring(57, 10).Trim 'locks
                '        values(5) = tmpstr2.Substring(68, 15).Trim 'open mode
                '        values(6) = tmpstr2.Substring(84) 'open file
                '        
                '    End If

                lsttest.Items.Add(tmpstr2)  '<-------------- Replace with your list box

            End If



        Loop Until tmpstr2 Is Nothing


        If Not _Process.HasExited Then
            _Process.Kill()
        End If

        sIn.Close()
        sErr.Close()
        _Process.Close()
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sr101880Commented:
The link to that page was http://www.codeproject.com/dotnet/OpenFiles.asp

:-)
0
sr101880Commented:
Was this what you were looking for?
0
narmi2Author Commented:
Thanks! :D
0
sr101880Commented:
Your welcome! I even managed to teach myself something new in the process.  :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.