Creating Resource Files
The .NET Framework's base class library provides several classes in the System.Resources Namespace to help you create and manipulate resources in your applications. For a detailed description of how to use each class, click on the link to view the corresponding reference topic.
Class | Description |
---|---|
Reads resources from a stream. |
|
Writes resources to a stream. |
|
Reads resources in the runtime default binary resource file format. This is a default implementation of the IResourceReader interface. |
|
Writes resources in the runtime default binary resource file format. This is a default implementation of the IResourceWriter interface. |
|
Provides the capability to retrieve resources created from .resx files. This is a specialized implementation of the IResourceReader interface. |
|
Provides the capability to create a .resx file from specified resources. This is a specialized implementation of IResourceWriter. Use the Resource File Generator (Resgen.exe) to convert the .resx file to the .resources file format. |
You can create resource files in three different ways. If your resource will only contain string data, the simplest method is to create a text file by hand. If your resources will contain objects or a combination of strings and objects, you must create either a .resx file or a .resources file. For information about each file type, click the corresponding link.
Note |
---|
Do not use resource files to store passwords, security-sensitive information, or private data. |
To write persisted objects to a resource file the objects must be serializable. .NET Framework types such as string and integer, and a group of Windows Forms types such as Bitmap, Icon, and Image, can be deserialized by semi-trusted code. For other types to be deserialized, the code that calls them must be granted the SecurityPermission with the SerializationFormatter flag specified.
Resources marked as private are accessible only in the assembly in which they are placed. Because a satellite assembly contains no code, resources private to it become unavailable through any mechanism. Therefore, resources in satellite assemblies should always be public so that they are accessible from your main assembly. Resources embedded in your main assembly are accessible to your main assembly, whether private or public.
Your application should never depend on resources from an assembly that you did not create and ship with your application. Dependencies on third-party resources can cause your application to fail as there is no guarantee that these resources will be present in a future release or in-place service pack.
Only .resources files should be embedded in common language runtime assemblies and satellite assemblies. The Resource File Generator (Resgen.exe) converts text (.txt) files and XML-based resource (.resx) files into .resources files by wrapping methods implemented by the ResourceReader, ResourceWriter, ResXResourceReader, and ResXResourceWriter classes. You can also use Resgen.exe to convert .resources files back into .resx and .txt files.
The Microsoft .NET Framework SDK provides several samples that illustrate how to work with .resource files. See Resources in Applications, the ASP.NET QuickStart Localization sample, and the Common Tasks QuickStart How To: Resources.