Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

ResourceWriter.Generate Method

Saves all resources to the output stream in the system default format.

[Visual Basic]
Public Overridable Sub Generate() Implements _
   IResourceWriter.Generate
[C#]
public virtual void Generate();
[C++]
public: virtual void Generate();
[JScript]
public function Generate();

Implements

IResourceWriter.Generate

Exceptions

Exception Type Condition
IOException An I/O error occurred.
SerializationException An error occurred during serialization of the object.
InvalidOperationException This ResourceWriter has been closed and its Hashtable is unavailable.

Remarks

Generate can only be called once, after all calls to AddResource have been made. If an exception occurs while writing the resources, the output stream is closed since the information written is most likely invalid.

Generate does not close the output stream in normal cases. Unless you are combining extra data with your .resources file or need access to the stream afterwards, you should call Close after calling Generate, or simply call Close.

Example

[Visual Basic] 
Imports System
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")

      ' Generate the resources, and close the writer.
      writer.Generate()
      writer.Close()
   End Sub

End Class

[C#] 
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");

       // Generate the resources, and close the writer.
       writer.Generate();
       writer.Close();
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Resources;
using namespace System::IO;

int main() {
    // Create a file stream to encapsulate items.resources.
    FileStream* fs = new FileStream(S"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(S"String 1", S"First String");
    writer->AddResource(S"String 2", S"Second String");
    writer->AddResource(S"String 3", S"Third String");

    // Generate the resources, and close the writer.
    writer->Generate();
    writer->Close();
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

ResourceWriter Class | ResourceWriter Members | System.Resources Namespace | XML and SOAP Serialization

Show:
© 2015 Microsoft