IndentedTextWriter 類別

定義

提供可以使用定位點字串 (String) 語彙基元 (Token) 縮排新行的文字寫入器。

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()

非同步執行與釋放 (Free)、釋放 (Release) 或重設 Unmanaged 資源相關聯之由應用程式定義的工作。

DisposeAsync()

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

(繼承來源 TextWriter)
Equals(Object)

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

(繼承來源 Object)
Flush()

清除資料流。

FlushAsync()

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

FlushAsync()

以非同步的方式清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。

(繼承來源 TextWriter)
FlushAsync(CancellationToken)

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

FlushAsync(CancellationToken)

以非同步的方式清除目前寫入器的所有緩衝區,並造成任何緩衝資料都寫入基礎裝置。

(繼承來源 TextWriter)
GetHashCode()

做為預設雜湊函式。

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

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetType()

取得目前執行個體的 Type

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

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
OutputTabs()

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

OutputTabsAsync()

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

ToString()

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

(繼承來源 Object)
Write(Boolean)

將布林值 (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(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(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)

設定如何執行從非同步可處置項目傳回的工作 await。

適用於