Console.SetIn(TextReader) Console.SetIn(TextReader) Console.SetIn(TextReader) Console.SetIn(TextReader) Method

定義

In プロパティを、指定した TextReader オブジェクトに設定します。Sets the In property to the specified TextReader object.

public:
 static void SetIn(System::IO::TextReader ^ newIn);
public static void SetIn (System.IO.TextReader newIn);
static member SetIn : System.IO.TextReader -> unit
Public Shared Sub SetIn (newIn As TextReader)

パラメーター

newIn
TextReader TextReader TextReader TextReader

新しい標準入力であるストリーム。A stream that is the new standard input.

例外

呼び出し元に、必要なアクセス許可がありません。The caller does not have the required permission.

SetIn メソッドの使用例を次に示します。The following example illustrates the use of the SetIn method. これにより、文字列内の 4 個の連続する空白文字がタブ文字に置き換えられます。It replaces four consecutive space characters in a string with a tab character. これを実行するには、2つのコマンドライン引数を指定する必要があります。To run it, you must supply two command line arguments. 1 つ目は、標準入力ストリームのリダイレクト先となる既存のテキスト ファイルの名前です。The first is the name of an existing text file to redirect the standard input stream to. 2 つ目は、標準出力ストリームのリダイレクト先となるファイルの名前です。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.

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;
}

using System;
using System.IO;

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 {
            // 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(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;
    }
}
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

注釈

既定では、 Inプロパティは標準入力ストリームに設定されます。By default, the In property is set to the standard input stream.

をカプセル化するを使用しStreamReaderて、ファイルからの入力を受け取ることFileStreamができます。A StreamReader that encapsulates a FileStream can be used to receive input from a file.

セキュリティ

SecurityPermission
アンマネージコードを呼び出すための。for calling unmanaged code. 関連付けられた列挙型:UnmanagedCodeAssociated enumeration: UnmanagedCode

適用対象

こちらもご覧ください