語言

ResourceWriter.Dispose 方法

定義

允許使用者關閉資源檔案或串流,明確釋放資源。

public:
 virtual void Dispose();
public void Dispose();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()

實作

例外狀況

發生了 I/O 錯誤。

在物件序列化過程中發生錯誤。

範例

以下程式碼範例使用該 Dispose 方法將類別中 ResourceWriter 的所有資源物件寫入輸出串流。 程式碼接著關閉寫入者,讓寫入者的資源可用於其他程序。

using System;
using System.Resources;
using System.IO;

public class WriteResources 
{
    public static void Main(string[] args) 
    {  
        // Create a file stream to encapsulate items.resources.
        FileStream fs = new FileStream("items.resources", 
           FileMode.OpenOrCreate,FileAccess.Write);

        // Open a resource writer to write from the stream.
        IResourceWriter writer = new ResourceWriter(fs);
    
        // Add resources to the resource writer.
        writer.AddResource("String 1", "First String");
        writer.AddResource("String 2", "Second String");
        writer.AddResource("String 3", "Third String");

        // Write the resources to the stream,  
        // and clean up all resources associated with the writer.
        // Calling Dispose is equivalent to calling Close.
        writer.Dispose();
    }
}
Imports System.Resources
Imports System.IO

Public Class WriteResources

  Public Shared Sub Main(ByVal args() As String)
      ' Create a file stream to encapsulate items.resources.
      Dim fs As New FileStream("items.resources", _
         FileMode.OpenOrCreate, FileAccess.Write)

      ' Open a resource writer to write from the stream.
      Dim writer = New ResourceWriter(fs)

      ' Add resources to the resource writer.
      writer.AddResource("String 1", "First String")
      writer.AddResource("String 2", "Second String")
      writer.AddResource("String 3", "Third String")

      ' Write the resources to the stream,  
      ' and clean up all resources associated with the writer.
      ' Calling Dispose is equivalent to calling Close.
      writer.Dispose()
   End Sub

End Class

備註

此方法的實作與 Close相同。

適用於

另請參閱