?
Solved

Help with ProcessStartInfo - RedirectStandardInput & RedirectStandardOutput

Posted on 2006-04-13
10
Medium Priority
?
296 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:narmi2
  • 7
  • 3
10 Comments
 
LVL 4

Expert Comment

by:sr101880
ID: 16444873
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
 
LVL 1

Author Comment

by:narmi2
ID: 16445761
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16446125
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
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
LVL 1

Author Comment

by:narmi2
ID: 16446216
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16446302
sorry, I was just trying to keep things simple.  I will get back to you shortly with some more info. :-)
0
 
LVL 4

Accepted Solution

by:
sr101880 earned 2000 total points
ID: 16449469
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
 
LVL 4

Expert Comment

by:sr101880
ID: 16449486
The link to that page was http://www.codeproject.com/dotnet/OpenFiles.asp

:-)
0
 
LVL 4

Expert Comment

by:sr101880
ID: 16454648
Was this what you were looking for?
0
 
LVL 1

Author Comment

by:narmi2
ID: 16455288
Thanks! :D
0
 
LVL 4

Expert Comment

by:sr101880
ID: 16455399
Your welcome! I even managed to teach myself something new in the process.  :-)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
In this video I will demonstrate how to set up Nine, which I now consider the best alternative email app to Touchdown.
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question