IndentedTextWriter 類別

定義

提供文字寫入器,可依索引卷標字串標記縮排新行。

public ref class IndentedTextWriter : System::IO::TextWriter
public class IndentedTextWriter : System.IO.TextWriter
type IndentedTextWriter = class
    inherit TextWriter
Public Class IndentedTextWriter
Inherits TextWriter
繼承
IndentedTextWriter

範例

下列程式代碼範例示範如何使用 IndentedTextWriter 在不同的縮排層級撰寫文字。

#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>

using namespace System;
using namespace System::CodeDom;
using namespace System::CodeDom::Compiler;
using namespace System::ComponentModel;
using namespace System::IO;
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::TextBox^ textBox1;

   String^ CreateMultilevelIndentString()
   {
      
      // Creates a TextWriter to use as the base output writer.
      System::IO::StringWriter^ baseTextWriter = gcnew System::IO::StringWriter;
      
      // Create an IndentedTextWriter and set the tab string to use 
      // as the indentation string for each indentation level.
      System::CodeDom::Compiler::IndentedTextWriter^ indentWriter = gcnew IndentedTextWriter( baseTextWriter,"    " );
      
      // Sets the indentation level.
      indentWriter->Indent = 0;
      
      // Output test strings at stepped indentations through a recursive loop method.
      WriteLevel( indentWriter, 0, 5 );
      
      // Return the resulting string from the base StringWriter.
      return baseTextWriter->ToString();
   }


   void WriteLevel( IndentedTextWriter^ indentWriter, int level, int totalLevels )
   {
      
      // Output a test string with a new-line character at the end.
      indentWriter->WriteLine( "This is a test phrase. Current indentation level: {0}", level );
      
      // If not yet at the highest recursion level, call this output method for the next level of indentation.
      if ( level < totalLevels )
      {
         
         // Increase the indentation count for the next level of indented output.
         indentWriter->Indent++;
         
         // Call the WriteLevel method to write test output for the next level of indentation.
         WriteLevel( indentWriter, level + 1, totalLevels );
         
         // Restores the indentation count for this level after the recursive branch method has returned.
         indentWriter->Indent--;
      }
      else
      // Outputs a string using the WriteLineNoTabs method.
            indentWriter->WriteLineNoTabs( "This is a test phrase written with the IndentTextWriter.WriteLineNoTabs method." );
      // Outputs a test string with a new-line character at the end.
      indentWriter->WriteLine( "This is a test phrase. Current indentation level: {0}", level );
   }


   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      textBox1->Text = CreateMultilevelIndentString();
   }


public:
   Form1()
   {
      System::Windows::Forms::Button^ button1 = gcnew System::Windows::Forms::Button;
      this->textBox1 = gcnew System::Windows::Forms::TextBox;
      this->SuspendLayout();
      this->textBox1->Anchor = (System::Windows::Forms::AnchorStyles)(System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom | System::Windows::Forms::AnchorStyles::Left | System::Windows::Forms::AnchorStyles::Right);
      this->textBox1->Location = System::Drawing::Point( 8, 40 );
      this->textBox1->Multiline = true;
      this->textBox1->Name = "textBox1";
      this->textBox1->Size = System::Drawing::Size( 391, 242 );
      this->textBox1->TabIndex = 0;
      this->textBox1->Text = "";
      button1->Location = System::Drawing::Point( 11, 8 );
      button1->Name = "button1";
      button1->Size = System::Drawing::Size( 229, 23 );
      button1->TabIndex = 1;
      button1->Text = "Generate string using IndentedTextWriter";
      button1->Click += gcnew System::EventHandler( this, &Form1::button1_Click );
      this->AutoScaleBaseSize = System::Drawing::Size( 5, 13 );
      this->ClientSize = System::Drawing::Size( 407, 287 );
      this->Controls->Add( button1 );
      this->Controls->Add( this->textBox1 );
      this->Name = "Form1";
      this->Text = "IndentedTextWriter example";
      this->ResumeLayout( false );
   }

};


[STAThread]
int main()
{
   Application::Run( gcnew Form1 );
}
using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace IndentedTextWriterExample
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1;

        private string CreateMultilevelIndentString()
        {
            // Creates a TextWriter to use as the base output writer.
            System.IO.StringWriter baseTextWriter = new System.IO.StringWriter();

            // Create an IndentedTextWriter and set the tab string to use
            // as the indentation string for each indentation level.
            System.CodeDom.Compiler.IndentedTextWriter indentWriter = new IndentedTextWriter(baseTextWriter, "    ");

            // Sets the indentation level.
            indentWriter.Indent = 0;

            // Output test strings at stepped indentations through a recursive loop method.
            WriteLevel(indentWriter, 0, 5);

            // Return the resulting string from the base StringWriter.
            return baseTextWriter.ToString();
        }

        private void WriteLevel(IndentedTextWriter indentWriter, int level, int totalLevels)
        {
            // Output a test string with a new-line character at the end.
            indentWriter.WriteLine("This is a test phrase. Current indentation level: "+level.ToString());

            // If not yet at the highest recursion level, call this output method for the next level of indentation.
            if( level < totalLevels )
            {
                // Increase the indentation count for the next level of indented output.
                indentWriter.Indent++;

                // Call the WriteLevel method to write test output for the next level of indentation.
                WriteLevel(indentWriter, level+1, totalLevels);

                // Restores the indentation count for this level after the recursive branch method has returned.
                indentWriter.Indent--;
            }
            else
            {
                // Outputs a string using the WriteLineNoTabs method.
                indentWriter.WriteLineNoTabs("This is a test phrase written with the IndentTextWriter.WriteLineNoTabs method.");
            }

            // Outputs a test string with a new-line character at the end.
            indentWriter.WriteLine("This is a test phrase. Current indentation level: "+level.ToString());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            textBox1.Text = CreateMultilevelIndentString();
        }

        public Form1()
        {
            System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(8, 40);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(391, 242);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "";
            button1.Location = new System.Drawing.Point(11, 8);
            button1.Name = "button1";
            button1.Size = new System.Drawing.Size(229, 23);
            button1.TabIndex = 1;
            button1.Text = "Generate string using IndentedTextWriter";
            button1.Click += new System.EventHandler(this.button1_Click);
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(407, 287);
            this.Controls.Add(button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "IndentedTextWriter example";
            this.ResumeLayout(false);
        }

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }
}
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.ComponentModel
Imports System.IO
Imports System.Windows.Forms

Public Class Form1
   Inherits System.Windows.Forms.Form
   Private textBox1 As System.Windows.Forms.TextBox 
   
   Private Function CreateMultilevelIndentString() As String
        ' Create a TextWriter to use as the base output writer.
        Dim baseTextWriter As New System.IO.StringWriter
      
        ' Create an IndentedTextWriter and set the tab string to use 
        ' as the indentation string for each indentation level.
        Dim indentWriter = New IndentedTextWriter(baseTextWriter, "    ")

        ' Set the indentation level.
        indentWriter.Indent = 0

        ' Output test strings at stepped indentations through a recursive loop method.
        WriteLevel(indentWriter, 0, 5)
      
        ' Return the resulting string from the base StringWriter.
        Return baseTextWriter.ToString()
    End Function

    Private Sub WriteLevel(ByVal indentWriter As IndentedTextWriter, ByVal level As Integer, ByVal totalLevels As Integer)
        ' Outputs a test string with a new-line character at the end.
        indentWriter.WriteLine(("This is a test phrase. Current indentation level: " + level.ToString()))

        ' If not yet at the highest recursion level, call this output method for the next level of indentation.
        If level < totalLevels Then
            ' Increase the indentation count for the next level of indented output.
            indentWriter.Indent += 1

            ' Call the WriteLevel method to write test output for the next level of indentation.
            WriteLevel(indentWriter, level + 1, totalLevels)

            ' Restores the indentation count for this level after the recursive branch method has returned.
            indentWriter.Indent -= 1

        Else
            ' Output a string using the WriteLineNoTabs method.
            indentWriter.WriteLineNoTabs("This is a test phrase written with the IndentTextWriter.WriteLineNoTabs method.")
        End If

        ' Outputs a test string with a new-line character at the end.
        indentWriter.WriteLine(("This is a test phrase. Current indentation level: " + level.ToString()))
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        textBox1.Text = CreateMultilevelIndentString()
    End Sub

    Public Sub New()
        Dim button1 As New System.Windows.Forms.Button
        Me.textBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        Me.textBox1.Anchor = CType(System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.textBox1.Location = New System.Drawing.Point(8, 40)
        Me.textBox1.Multiline = True
        Me.textBox1.Name = "textBox1"
        Me.textBox1.Size = New System.Drawing.Size(391, 242)
        Me.textBox1.TabIndex = 0
        Me.textBox1.Text = ""
        button1.Location = New System.Drawing.Point(11, 8)
        button1.Name = "button1"
        button1.Size = New System.Drawing.Size(229, 23)
        button1.TabIndex = 1
        button1.Text = "Generate string using IndentedTextWriter"
        AddHandler button1.Click, AddressOf Me.button1_Click
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(407, 287)
        Me.Controls.Add(button1)
        Me.Controls.Add(Me.textBox1)
        Me.Name = "Form1"
        Me.Text = "IndentedTextWriter example"
        Me.ResumeLayout(False)
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New Form1)
    End Sub
End Class

備註

IndentedTextWriter 藉由提供插入索引標籤字串並追蹤目前縮排層級的方法,來擴充 TextWriter。 使用多個縮排層級格式化的文字對於產生的程式代碼很有用,因此 CodeDOM 程式代碼產生器實作會使用此類別。

索引標籤字串是每個縮排所組成的字串。 索引標籤字串通常包含空格符。

注意

這個類別包含適用於所有成員之類別層級的連結需求和繼承需求。 當立即呼叫端或衍生類別沒有完全信任許可權時,就會擲回 SecurityException。 如需安全性需求的詳細資訊,請參閱 連結需求繼承需求

建構函式

IndentedTextWriter(TextWriter)

使用指定的文字寫入器和預設索引標籤字串,初始化 IndentedTextWriter 類別的新實例。

IndentedTextWriter(TextWriter, String)

使用指定的文字寫入器和索引標籤字串,初始化 IndentedTextWriter 類別的新實例。

欄位

CoreNewLine

儲存用於這個 TextWriter的新行字元。

(繼承來源 TextWriter)
DefaultTabString

指定預設索引標籤字串。 此欄位是常數。

屬性

Encoding

取得要使用的文字寫入器編碼。

FormatProvider

取得控制項格式設定的物件。

(繼承來源 TextWriter)
Indent

取得或設定要縮排的空格數目。

InnerWriter

取得要使用的 TextWriter

NewLine

取得或設定要使用的新行字元。

方法

Close()

關閉正在寫入的檔。

CreateObjRef(Type)

建立物件,其中包含產生用來與遠端物件通訊之 Proxy 所需的所有相關信息。

(繼承來源 MarshalByRefObject)
Dispose()

釋放 TextWriter 物件所使用的所有資源。

(繼承來源 TextWriter)
Dispose(Boolean)

釋放 TextWriter 所使用的 Unmanaged 資源,並選擇性地釋放 Managed 資源。

(繼承來源 TextWriter)
DisposeAsync()

以異步方式執行與釋放、釋放或重設非受控資源相關聯的應用程式定義工作。

DisposeAsync()

以異步方式釋放 TextWriter 物件所使用的所有資源。

(繼承來源 TextWriter)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Flush()

排清數據流。

FlushAsync()

以異步方式清除此 IndentedTextWriter 的所有緩衝區,並導致任何緩衝的數據寫入基礎裝置。

FlushAsync()

以異步方式清除目前寫入器的所有緩衝區,並導致任何緩衝的數據寫入基礎裝置。

(繼承來源 TextWriter)
FlushAsync(CancellationToken)

以異步方式清除此 IndentedTextWriter 的所有緩衝區,並導致任何緩衝的數據寫入基礎裝置。

FlushAsync(CancellationToken)

以異步方式清除目前寫入器的所有緩衝區,並導致任何緩衝的數據寫入基礎裝置。

(繼承來源 TextWriter)
GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個實例存留期原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前實例的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個實例的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 對象的淺層複本。

(繼承來源 MarshalByRefObject)
OutputTabs()

根據 Indent 屬性,針對每個縮排層級輸出索引標籤字串一次。

OutputTabsAsync()

根據目前的 Indent,以異步方式將索引標籤輸出至基礎 TextWriter

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
Write(Boolean)

將布爾值的文字表示寫入文字數據流。

Write(Char)

將字元寫入文字數據流。

Write(Char[])

將字元陣列寫入文字數據流。

Write(Char[], Int32, Int32)

將字元的子陣列寫入文字數據流。

Write(Decimal)

將十進位值的文字表示寫入文字數據流。

(繼承來源 TextWriter)
Write(Double)

將 Double 的文字表示寫入文字數據流。

Write(Int32)

將整數的文字表示寫入文字數據流。

Write(Int64)

將8位元組整數的文字表示寫入文字數據流。

Write(Object)

將物件的文字表示寫入文字數據流。

Write(ReadOnlySpan<Char>)

將字元範圍寫入文字數據流。

(繼承來源 TextWriter)
Write(Single)

將Single的文字表示寫入文字數據流。

Write(String)

將指定的字串寫入文字數據流。

Write(String, Object)

使用與指定相同的語意來寫出格式化字串。

Write(String, Object, Object)

使用與指定相同的語意來寫出格式化字串。

Write(String, Object, Object, Object)

使用與 Format(String, Object, Object, Object) 方法相同的語意,將格式化字串寫入文字數據流。

(繼承來源 TextWriter)
Write(String, Object[])

使用與指定相同的語意來寫出格式化字串。

Write(String, ReadOnlySpan<Object>)

使用與指定相同的語意來寫出格式化字串。

Write(String, ReadOnlySpan<Object>)

使用與 Format(String, ReadOnlySpan<Object>)相同的語意,將格式化字串寫入文字數據流。

(繼承來源 TextWriter)
Write(StringBuilder)

將字串產生器寫入至文字數據流。

(繼承來源 TextWriter)
Write(UInt32)

將 4 位元組無符號整數的文字表示寫入文字數據流。

(繼承來源 TextWriter)
Write(UInt64)

將8位元組無符號整數的文字表示寫入文字數據流。

(繼承來源 TextWriter)
WriteAsync(Char)

以異步方式將指定的 Char 寫入基礎 TextWriter,並在每一行開頭插入索引標籤。

WriteAsync(Char)

以異步方式將字元寫入文字數據流。

(繼承來源 TextWriter)
WriteAsync(Char[])

以異步方式將字元陣列寫入文字數據流。

(繼承來源 TextWriter)
WriteAsync(Char[], Int32, Int32)

以異步方式將指定的 Char數目從指定的緩衝區寫入基礎 TextWriter,從指定的索引處開始,並在每一個新行的開頭輸出索引卷標。

WriteAsync(Char[], Int32, Int32)

以異步方式將字元的子數位寫入文字數據流。

(繼承來源 TextWriter)
WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

以異步方式將指定的字元寫入基礎 TextWriter,並在每一行開頭插入索引標籤。

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

以異步方式將字元記憶體區域寫入文字數據流。

(繼承來源 TextWriter)
WriteAsync(String)

以異步方式將指定的字串寫入基礎 TextWriter,並在每一行開頭插入索引標籤。

WriteAsync(String)

以異步方式將字串寫入文字數據流。

(繼承來源 TextWriter)
WriteAsync(StringBuilder, CancellationToken)

以異步方式將指定 StringBuilder 的內容寫入基礎 TextWriter,並在每一行開頭插入索引標籤。

WriteAsync(StringBuilder, CancellationToken)

以異步方式將字串產生器寫入至文字數據流。

(繼承來源 TextWriter)
WriteLine()

寫入行終止符。

WriteLine(Boolean)

將布爾值的文字表示,後面接著行終止符寫入文字數據流。

WriteLine(Char)

將字元,後面接著行終止符寫入文字數據流。

WriteLine(Char[])

將字元陣列,後面接著行終止符寫入文字數據流。

WriteLine(Char[], Int32, Int32)

將字元的子陣列,後面接著行終止符寫入文字數據流。

WriteLine(Decimal)

將十進位值的文字表示寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLine(Double)

將 Double 的文字表示,後面接著行終止符寫入文字數據流。

WriteLine(Int32)

將整數的文字表示,後面接著行終止符寫入文字數據流。

WriteLine(Int64)

將8位元組整數的文字表示,後面接著行終止符寫入文字數據流。

WriteLine(Object)

將 物件的文字表示,後面接著行終止符寫入文字數據流。

WriteLine(ReadOnlySpan<Char>)

將字元範圍的文字表示寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLine(Single)

將Single的文字表示,後面接著行終止符寫入文字數據流。

WriteLine(String)

將指定的字串,後面接著行終止符寫入文字數據流。

WriteLine(String, Object)

使用與指定相同的語意,寫出格式化字串,後面接著行終止符。

WriteLine(String, Object, Object)

使用與指定相同的語意,寫出格式化字串,後面接著行終止符。

WriteLine(String, Object, Object, Object)

使用與 Format(String, Object)相同的語意,將格式化字串和新行寫出至文字數據流。

(繼承來源 TextWriter)
WriteLine(String, Object[])

使用與指定相同的語意,寫出格式化字串,後面接著行終止符。

WriteLine(String, ReadOnlySpan<Object>)

使用與指定相同的語意,寫出格式化字串,後面接著行終止符。

WriteLine(String, ReadOnlySpan<Object>)

使用與 Format(String, ReadOnlySpan<Object>)相同的語意,將格式化字串和新行寫出至文字數據流。

(繼承來源 TextWriter)
WriteLine(StringBuilder)

將字串產生器的文字表示寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLine(UInt32)

將 UInt32 的文字表示,後面接著行終止符寫入文字數據流。

WriteLine(UInt64)

將8位元組無符號整數的文字表示寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLineAsync()

以異步方式將行終止符寫入基礎 TextWriter

WriteLineAsync()

以異步方式將行終止符寫入文字數據流。

(繼承來源 TextWriter)
WriteLineAsync(Char)

以異步方式將指定的 Char 寫入基礎 TextWriter 後面接著行終止符,並在每一行開頭插入索引標籤。

WriteLineAsync(Char)

以異步方式將字元寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLineAsync(Char[])

以異步方式將字元陣列寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLineAsync(Char[], Int32, Int32)

從指定的緩衝區後面接著行終止符,以異步方式將指定的字元數寫入至基礎 TextWriter,從緩衝區內的指定索引處開始,在每個行的開頭插入索引卷標。

WriteLineAsync(Char[], Int32, Int32)

以異步方式將字元的子陣列寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以異步方式將指定的字元,後面接著行終止符寫入基礎 TextWriter,並在每一行開頭插入索引卷標。

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以異步方式將字元記憶體區域的文字表示寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLineAsync(String)

以異步方式將指定的字串,後面接著行終止符寫入基礎 TextWriter,並在每一行開頭插入索引卷標。

WriteLineAsync(String)

以異步方式將字串寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLineAsync(StringBuilder, CancellationToken)

以異步方式將指定 StringBuilder 的內容,後面接著行終止符寫入基礎 TextWriter,並在每一行開頭插入索引標籤。

WriteLineAsync(StringBuilder, CancellationToken)

以異步方式將字串產生器的文字表示寫入文字數據流,後面接著行終止符。

(繼承來源 TextWriter)
WriteLineNoTabs(String)

將指定的字串寫入沒有索引標籤的行。

WriteLineNoTabsAsync(String)

以異步方式將指定的字串寫入基礎 TextWriter,而不插入索引標籤。

明確介面實作

IDisposable.Dispose()

如需此成員的描述,請參閱 Dispose()

(繼承來源 TextWriter)

擴充方法

ConfigureAwait(IAsyncDisposable, Boolean)

設定如何執行從異步可處置專案傳回的工作等候。

適用於