Home  |  Delphi .net Home  |  System.Console  |  SetIn Method
  
SetIn  
Method  
Assigns a new Standard Input Stream reader
Console Class
System NameSpace
CF1.  Procedure SetIn ( NewReader : TextReader ) ; Static;
CF : Methods with this mark are Compact Framework Compatible
Description
Assigns the In property to NewReader, allowing a different source of text lines for a Console application.
 
As in the example, this can be used to read from a text file instead.
Notes
Static methods are not methods of an object - they are simply class functions or procedures available at any time.
Microsoft MSDN Links
System
System.Console
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 
 
A simple example
program Project1;
{$APPTYPE CONSOLE}

uses
  System.IO;

var
  fileName  : String;

  consoleIn : TextReader;
  reader    : TextReader;
  writer    : TextWriter;

  text      : String;

begin
  // Create a test text file
  fileName := 'C:DelphiBasics.txt';
  if not System.IO.File.Exists(fileName)
  then System.IO.File.CreateText(fileName);

  // Open the file
  writer := StreamWriter.Create(fileName);

  // Write some lines to the file
  writer.WriteLine('Hello');
  writer.WriteLine('World');

  // Close the file
  writer.Close;

  // Create a reader stream for this file
  reader := System.IO.File.OpenText(fileName);

  // Save the existing console reader
  consoleIn := Console.In;

  // Assign the Console In stream to this file
  Console.SetIn(reader);

  // Now read from the console!
  text := Console.ReadLine;
  Console.WriteLine(text);
  text := Console.ReadLine;
  Console.WriteLine(text);

  // Assign the In stream back to the console
  Console.SetIn(consoleIn);

  Console.ReadLine;
end.
   Hello
   World
 
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Contact the author