Debug.Flush 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
刷新输出缓冲区,并使放入缓冲区中的数据写入 Listeners 集合。
public:
 static void Flush();[System.Diagnostics.Conditional("DEBUG")]
public static void Flush ();[<System.Diagnostics.Conditional("DEBUG")>]
static member Flush : unit -> unitPublic Shared Sub Flush ()- 属性
示例
以下示例创建一个名为 TextWriterTraceListener 的 myTextListener。 
              myTextListener
              FileStream使用调用 myFileStream 的 写入名为 的文件TestFile.txt。 该示例创建流,打开文件(如果存在)或创建一个新文件,将一行文本写入文件,然后刷新并关闭输出。
// Specify /DDEBUG when compiling.
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Diagnostics;
void main()
{
     #if defined(DEBUG)
    // Create a new stream object for an output file named TestFile.txt.
    FileStream^ myFileStream = 
        gcnew FileStream( "TestFile.txt", FileMode::Append );
   
    // Add the stream object to the trace listeners.
    TextWriterTraceListener^ myTextListener = 
        gcnew TextWriterTraceListener( myFileStream );
    Debug::Listeners->Add( myTextListener );
   
    // Write output to the file.
    Debug::WriteLine( "Test output" );
   
    // Flush and close the output stream.
    Debug::Flush();
    Debug::Close();
    #endif
}
// Specify /d:DEBUG when compiling.
using System;
using System.IO;
using System.Diagnostics;
class Test
{
    static void Main()
    {
        // Create a new stream object for an output file named TestFile.txt.
        using (FileStream myFileStream =
            new FileStream("TestFile.txt", FileMode.Append))
        {
            // Add the stream object to the trace listeners.
            TextWriterTraceListener myTextListener =
                new TextWriterTraceListener(myFileStream);
            Debug.Listeners.Add(myTextListener);
            // Write output to the file.
            Debug.WriteLine("Test output");
            // Flush and close the output stream.
            Debug.Flush();
            Debug.Close();
        }
    }
}
' Specify /d:DEBUG=True when compiling.
Imports System.IO
Imports System.Diagnostics
Class Test
    
    Shared Sub Main()
    
        ' Create a new stream object for an output file named TestFile.txt.
        Using myFileStream As New FileStream("TestFile.txt", FileMode.Append)
        
            ' Add the stream object to the trace listeners. 
            Dim myTextListener As New TextWriterTraceListener(myFileStream)
            Debug.Listeners.Add(myTextListener)
            
            ' Write output to the file.
            Debug.WriteLine("Test output")
            
            ' Flush and close the output stream.
            Debug.Flush()
            Debug.Close()
        
        End Using
        
    End Sub
End Class
注解
除非显式调用 Flush 或 Close,否则刷新流不会刷新其基础编码器。 将 设置为 AutoFlushtrue 表示数据将从缓冲区刷新到流,但不会刷新编码器状态。 这允许编码器将其状态保留 (部分字符) ,以便可以正确编码下一个字符块。 此方案会影响 UTF8 和 UTF7,其中某些字符只能在编码器收到相邻字符后进行编码。