Solved

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

Posted on 2008-10-21
9
4,161 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

724 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