Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Console.ReadLine Method

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

[Visual Basic]
Public Shared Function ReadLine() As String
[C#]
public static string ReadLine();
[C++]
public: static String* ReadLine();
[JScript]
public static function ReadLine() : String;

Return Value

The next line from the input stream, or a null reference (Nothing in Visual Basic) if no more characters are available.

Exceptions

Exception Type Condition
IOException An I/O error occurred.
OutOfMemoryException There is insufficient memory to allocate a buffer for the returned string.

Remarks

A line is defined as a sequence of characters followed by a carriage return (hexadecimal 0x000d), a line feed (hexadecimal 0x000a), or Environment.NewLine. The returned string does not contain the terminating character(s).

If this method throws OutOfMemoryException, the reader's position in the underlying Stream 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 produce robust code you should use the Read method and store the read characters in a preallocated buffer.

Example

The following code sample illustrates the usage of ReadLine:

[Visual Basic] 
Public Class InsertTabs
   Private Const tabSize As Integer = 4
   Private Const usageText As String = "Usage: INSERTTABS inputfile.txt outputfile.txt"
   
   'Entry point which delegates to C-style main Private Function
   Public Overloads Shared Sub Main()
      System.Environment.ExitCode = Main(System.Environment.GetCommandLineArgs())
   End Sub
   
   Overloads Public Shared Function Main(args() As String) As Integer
      Dim writer As StreamWriter = Nothing
      
      If args.Length < 3 Then
         Console.WriteLine(usageText)
         Return 1
      End If
      
      Try
         writer = New StreamWriter(args(2))
         Console.SetOut(writer)
         Console.SetIn(New StreamReader(args(1)))
      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
      line = Console.ReadLine()
      While Not line Is 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 'Main
End Class 'InsertTabs

[C#] 
public class InsertTabs {
    private const int tabSize = 4;
    private const string usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";
    public static int Main(string[] args) {
        StreamWriter writer = null;

        if (args.Length < 2) {
            Console.WriteLine(usageText);
            return 1;
        }

        try {
            writer = new StreamWriter(args[1]);
            Console.SetOut(writer);
            Console.SetIn(new StreamReader(args[0]));
        }
        catch(IOException e) {
            TextWriter errorWriter = Console.Error;
            errorWriter.WriteLine(e.Message);
            errorWriter.WriteLine(usageText);
            return 1;            
        }
        string line;
        while ((line = Console.ReadLine()) != null) {
            string newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
            Console.WriteLine(newLine);
        }
        writer.Close();
        // Recover the standard output stream so that a 
        // completion message can be displayed.
        StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
        standardOutput.AutoFlush = true;
        Console.SetOut(standardOutput);
        Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);
        return 0;
    }
}

[C++] 
int main() {
    String* args[] = Environment::GetCommandLineArgs();
    const int tabSize = 4;
    String* usageText = S"Usage: INSERTTABS inputfile.txt outputfile.txt";

    StreamWriter* writer = 0;

    if (args->Length < 3) {
        Console::WriteLine(usageText);
        return 1;
    }

    try {
        writer = new StreamWriter(args[2]);
        Console::SetOut(writer);
        Console::SetIn(new StreamReader(args[1]));
    }
    catch(IOException* e) {
        TextWriter* errorWriter = Console::Error;
        errorWriter->WriteLine(e->Message);
        errorWriter->WriteLine(usageText);
        return 1;            
    }
    String* line;
    while ((line = Console::ReadLine()) != 0) {
        String* newLine = line->Replace((S"")->PadRight(tabSize, ' '), S"\t");
        Console::WriteLine(newLine);
    }
    writer->Close();
    // Recover the standard output stream so that a 
    // completion message can be displayed.
    StreamWriter* standardOutput = new StreamWriter(Console::OpenStandardOutput());
    standardOutput->AutoFlush = true;
    Console::SetOut(standardOutput);
    Console::WriteLine(S"INSERTTABS has completed the processing of {0}.", args[1]);
    return 0;
}

[JScript] 
const tabSize = 4;
const usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt";

var writer : StreamWriter = null;
var args = Environment.GetCommandLineArgs();

if (args.Length != 3) {
    Console.WriteLine(usageText);
    Environment.Exit(1);
}

try {
    writer = new StreamWriter(args[2]);
    Console.SetOut(writer);
    Console.SetIn(new StreamReader(args[1]));
}
catch(e : IOException) {
    var errorWriter = Console.Error;
    errorWriter.WriteLine(e.Message);
    errorWriter.WriteLine(usageText);
    Environment.Exit(1);            
}
var line;
while ((line = Console.ReadLine()) != null) {
    var newLine = line.Replace(("").PadRight(tabSize, ' '), "\t");
    Console.WriteLine(newLine);
}
writer.Close();
// Recover the standard output stream so that a 
// completion message can be displayed.
var standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
Console.WriteLine("INSERTTABS has completed the processing of {0}.", args[0]);

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Console Class | Console Members | System Namespace | Read | Write | WriteLine

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft