Console::SetIn Method
Sets the In property to the specified TextReader object.
Assembly: mscorlib (in mscorlib.dll)
[HostProtectionAttribute(SecurityAction::LinkDemand, UI = true)] public: static void SetIn( TextReader^ newIn )
Parameters
- newIn
- Type: System.IO::TextReader
A stream that is the new standard input.
| Exception | Condition |
|---|---|
| ArgumentNullException | newIn is nullptr. |
| SecurityException | The caller does not have the required permission. |
By default, the In property is set to the standard input stream.
A StreamReader that encapsulates a FileStream can be used to receive input from a file.
Note |
|---|
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 code example illustrates the use of the SetIn method.
using namespace System; using namespace System::IO; int main() { array<String^>^args = Environment::GetCommandLineArgs(); const int tabSize = 4; String^ usageText = "Usage: INSERTTABS inputfile.txt outputfile.txt"; StreamWriter^ writer = nullptr; if ( args->Length < 3 ) { Console::WriteLine( usageText ); return 1; } try { // Attempt to open output file. writer = gcnew StreamWriter( args[ 2 ] ); // 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( gcnew 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()) != nullptr ) { String^ newLine = line->Replace( ((String^)"")->PadRight( tabSize, ' ' ), "\t" ); Console::WriteLine( newLine ); } writer->Close(); // Recover the standard output stream so that a // completion message can be displayed. StreamWriter^ standardOutput = gcnew StreamWriter( Console::OpenStandardOutput() ); standardOutput->AutoFlush = true; Console::SetOut( standardOutput ); Console::WriteLine( "INSERTTABS has completed the processing of {0}.", args[ 1 ] ); return 0; }
- SecurityPermission
for calling unmanaged code. Associated enumeration: SecurityPermissionFlag::UnmanagedCode
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note