Solved

How  do I access stdin and stdout in VB.net?

Posted on 2008-10-21
9
3,949 Views
Last Modified: 2012-05-05
I want to create myself a UNIX "Tee" type command, that is I read from stdin and send output to both stdout and a file.

I looked at system.io.streamreader.....but that doesn't seem to work.
0
Comment
Question by:Christopher Schene
  • 4
  • 4
9 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 22772023
0
 
LVL 3

Expert Comment

by:TechSinger
ID: 22772067
You do this by using the Console class.  It can be used directly without needing to declare a variable.
A very rudementry but functional example of its use is:

Sub Main()
Console.Write("What is you name: ")
Dim sInput As String = Console.ReadLine()
Console.WriteLine("Nice to meet you " + sInput + ".")
End Sub
If you compile it as a console application and make the Startup Object "Sub Main", you can then run this at a command line and redirect the input and the output of the executable.
Hope this helps.
0
 

Author Comment

by:Christopher Schene
ID: 22772098
This looks like VB 6 code
  '
    Sub GetCGIpostData()
 
        ' Read the standard input handle
        Dim llStdIn As FileAttrib = GetStdHandle(STD_INPUT_HANDLE)
        ' Get POSTed CGI data from STDIN
        Do
      dim lsBuff as String (1024)
            Dim llBytesRead As Integer = 1024          ' Tell it we want at least 1024 bytes
      If ReadFile(llStdIn, ByVal lsBuff, 1024, llBytesRead, ByVal 0&) Then
                ' Read the data
                ' Add the data to our string
                postData = postData & Left(lsBuff, llBytesRead)
                If llBytesRead < 1024 Then Exit Do
            Else
                Exit Do
            End If
        Loop
 
    End Sub

Open in new window

0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

Author Comment

by:Christopher Schene
ID: 22772115
tech TechSinger, how do I determine "end of stream"?

Sub Main()
Console.Write("What is you name: ")
Dim sInput As String = Console.ReadLine()
Console.WriteLine("Nice to meet you " + sInput + ".")
End Sub
0
 
LVL 3

Accepted Solution

by:
TechSinger earned 250 total points
ID: 22776402
The Console object doesn't have an end of stream propery per say, but you can use its In property to read the entire contents of the stdIn to a string variable.
To do this, you need to Import System.IO to be able to declare a TextReader variable.  You can then assign this variable to stdIn by using the Console object.  I have altered the code example as follows:

Imports System.IO
Module Module1
Sub Main()
Dim stdIn As TextReader = Console.In
Console.Write("What is you name: ")
Dim sInput As String = stdIn.ReadToEnd
Console.WriteLine("Nice to meet you " + sInput + ".")
End Sub
End Module
If you redirect stdIn to a text file, this will read the whole file into the sInput string.  If the console remains as the stdIn, it will continue accepting input until you type the EOF key combination (Ctrl-Z) on a new line and then Enter.
I hope this will work for you.
0
 

Author Comment

by:Christopher Schene
ID: 22777363
I got the attched to work, so you have the points
Module Module1
    Dim filename As String
    Sub Main()
        If My.Application.CommandLineArgs.Count = 1 Then
            filename = My.Application.CommandLineArgs.Item(0)
        Else
            MsgBox("You must declare a filename. Example: program.exe | tee filename")
            Exit Sub
        End If
 
 
        Try
            Dim s As String = Console.ReadLine()
            While s.Length > 0
                Console.WriteLine(s)
                writeToFile(s)
                s = Console.ReadLine()
            End While
        Catch ex As Exception
        End Try
 
    End Sub
 
    Private Sub writeToFile(ByVal s As String)
        Dim file As System.IO.StreamWriter
        file = FileIO.FileSystem.OpenTextFileWriter(filename, True)
        file.WriteLine(s)
        file.Close()
    End Sub
 
End Module

Open in new window

0
 

Author Closing Comment

by:Christopher Schene
ID: 31508513
I had to write the code and it exits via exception, but the approach is a simple and clear one. Good job!
0
 
LVL 3

Expert Comment

by:TechSinger
ID: 22778516
That looks like a workable solution.  You may want to identify the exception so that you can make sure that it is not something else that causes it.
Just for your information. If you use TextReader.ReadToEnd you can avoid causing an exception.  The disadvantage however is that if the console is used as the Standard Input, the EOF key (Ctrl-Z) has to typed at the last line which has to be the first character of the line.  That character is not needed if a file is used as stdIn.
I have altered your code to below to reflect this.

Imports System.IO
Module Module1
    Dim filename As String
    Sub Main()
        If My.Application.CommandLineArgs.Count = 1 Then
            filename = My.Application.CommandLineArgs.Item(0)
        Else
            MsgBox("You must declare a filename. Example: program.exe | tee filename")
            Exit Sub
        End If
        Dim stdIn As TextReader = Console.In
        Dim sLines() As String = stdIn.ReadToEnd.Replace(Chr(10), "").Split(Chr(13))
        Dim i As Integer
        For i = 0 To sLines.Length - 1
            Dim s As String = Console.ReadLine()
            If s.Length > 0 Then
                Console.WriteLine(s)
                writeToFile(s)
            End If
        Next
    End Sub
 
    Private Sub writeToFile(ByVal s As String)
        Dim file As System.IO.StreamWriter
        file = FileIO.FileSystem.OpenTextFileWriter(filename, True)
        file.WriteLine(s)
        file.Close()
    End Sub
 
End Module

Open in new window

0
 
LVL 3

Expert Comment

by:TechSinger
ID: 22778553
Sorry, I had one line that is wrong in the code.
Line 15 is - Dim s As String = Console.ReadLine()

It shoud be - Dim s As String = sLines(i)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

790 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