Console.SetOut Method (TextWriter)

 

Sets the Out property to the specified TextWriter object.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

<HostProtectionAttribute(SecurityAction.LinkDemand, UI := True)>
Public Shared Sub SetOut (
	newOut As TextWriter
)

Parameters

newOut
Type: System.IO.TextWriter

A stream that is the new standard output.

Exception Condition
ArgumentNullException

newOut is null.

SecurityException

The caller does not have the required permission.

By default, the Out property is set to the standard output stream.

A StreamWriter that encapsulates a FileStream can be used to send output to a file. For example:

Console.WriteLine("Hello World")
Dim fs As New FileStream("Test.txt", FileMode.Create)
' First, save the standard output.
Dim tmp as TextWriter = Console.Out
Dim sw As New StreamWriter(fs)
Console.SetOut(sw)
Console.WriteLine("Hello file")
Console.SetOut(tmp)
Console.WriteLine("Hello World")
sw.Close()

The following example illustrates the use of the SetOut method. It replaces four consecutive space characters in a string with a tab character. To run it, you must supply two command line arguments. The first is the name of an existing text file to redirect the standard input stream to. The second is the name of a file to redirect the standard output stream to. This file need not exist. If it does, its contents will be overwritten.

Imports System.IO

Public Class InsertTabs
   Private Const tabSize As Integer = 4
   Private Const usageText As String = "Usage: INSERTTABS inputfile.txt outputfile.txt"

   Public Shared Function Main(args() As String) As Integer
      Dim writer As StreamWriter = Nothing

      If args.Length < 2 Then
         Console.WriteLine(usageText)
         Return 1
      End If

      Try
         ' Attempt to open output file.
         writer = New StreamWriter(args(1))
         ' Redirect standard output from the console to the output file.
         Console.SetOut(writer)
         ' Redirect standard input from the console to the input file.
         Console.SetIn(New StreamReader(args(0)))
      Catch e As IOException
         Dim errorWriter As TextWriter = Console.Error
         errorWriter.WriteLine(e.Message)
         errorWriter.WriteLine(usageText)
         Return 1
      End Try

      Dim line As String = Console.ReadLine()
      While line IsNot Nothing
         Dim newLine As String = line.Replace("".PadRight(tabSize, " "c), ControlChars.Tab)
         Console.WriteLine(newLine)
         line = Console.ReadLine()
      End While
      writer.Close()
      ' Recover the standard output stream so that a 
      ' completion message can be displayed.
      Dim standardOutput As New StreamWriter(Console.OpenStandardOutput())
      standardOutput.AutoFlush = True
      Console.SetOut(standardOutput)
      Console.WriteLine("INSERTTABS has completed the processing of {0}.", args(0))
      Return 0
   End Function 
End Class

SecurityPermission

for calling unmanaged code. Associated enumeration: SecurityPermissionFlag.UnmanagedCode

.NET Framework
Available since 1.1
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
Show: