Export (0) Print
Expand All

Console.ReadLine Method

Reads the next line of characters from the standard input stream.

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

'Declaration
<HostProtectionAttribute(SecurityAction.LinkDemand, UI := True)> _
Public Shared Function ReadLine As String

Return Value

Type: System.String
The next line of characters from the input stream, or Nothing if no more lines are available.

ExceptionCondition
IOException

An I/O error occurred.

OutOfMemoryException

There is insufficient memory to allocate a buffer for the returned string.

ArgumentOutOfRangeException

The number of characters in the next line of characters is greater than Int32.MaxValue.

A line is defined as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a), or the value of the Environment.NewLine property. The returned string does not contain the terminating character(s). By default, the method reads input from a 256-character input buffer. Because this includes the Environment.NewLine character(s), the method can read lines that contain up to 254 characters. To read longer lines, call the OpenStandardInput(Int32) method.

If this method throws an OutOfMemoryException exception, the reader's position in the underlying Stream object is advanced by the number of characters the method was able to read, but the characters already read into the internal ReadLine buffer are discarded. Since the position of the reader in the stream cannot be changed, the characters already read are unrecoverable, and can be accessed only by reinitializing the TextReader. If the initial position within the stream is unknown or the stream does not support seeking, the underlying Stream also needs to be reinitialized. To avoid such a situation and to produce robust code, you should use the KeyAvailable property and ReadKey method and store the read characters in a pre-allocated buffer.

If the Ctrl+Z character is pressed when the method is reading input from the console, the method returns Nothing. This enables the user to prevent further keyboard input when the ReadLine method is called in a loop. The following example illustrates this scenario.

Module Example
   Public Sub Main()
      Dim line As String
      Console.WriteLine("Enter one or more lines of text (press CTRL+Z to exit):")
      Console.WriteLine()
      Do 
         Console.Write("   ")
         line = Console.ReadLine()
         If line IsNot Nothing Then Console.WriteLine("      " + line)
      Loop While line IsNot Nothing    
   End Sub 
End Module 
' The following displays possible output from this example: 
'       Enter one or more lines of text (press CTRL+Z to exit): 
'        
'          This is line #1. 
'             This is line #1. 
'          This is line #2 
'             This is line #2 
'          ^Z 
'        
'       >
NoteNote

The HostProtectionAttribute attribute applied to this type or member has the following Resources property value: UI. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following example requires two command line arguments: the name of an existing text file, and the name of a file to write the output to. It opens the existing text file and redirects the standard input from the keyboard to that file. It also redirects the standard output from the console to the output file. It then uses the Console.ReadLine method to read each line in the file, replaces every sequence of four spaces with a tab character, and uses the Console.WriteLine method to write the result to the output file.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft