Solved

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

Posted on 2008-10-21
9
4,024 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Title # Comments Views Activity
vb.net class 3 28
VB.NET String Settings and Temp Folder Question 3 68
ModalPopup  question 22 37
VBA: copy range dynamically based on config sheet v3 6 23
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that undeā€¦
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

685 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