Solved

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

Posted on 2008-10-21
9
3,746 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:cschene
  • 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:cschene
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
 

Author Comment

by:cschene
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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:cschene
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:cschene
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now