Adding and Editing Resources

Visual C# applications often include data that is not source code. Such data is referred to as a project resource and it can include binary data, text files, audio or video files, string tables, icons, images, XML files, or any other type of data that your application requires. Project resource data is stored in XML format in the .resx file (named Resources.resx by default) which can be opened in Solution Explorer. For more information about project resources, see Working with Resource Files.

Adding Resources to Projects

You can add resources to a project by clicking Add Existing Item on the Project menu, or by clicking the Add Resource button on the Resources page in Project Designer.

You can add resources to your project either as linked resources, which are external files, or as embedded resources, which are embedded directly into the .resx file.

  • When you add a linked resource, the .resx file that stores your project resource information includes only a relative path of the resource file on disk. If you add images, videos, or other complex files as linked resources, you can edit them by using a default editor that you associate with that file type in the Resource Designer.

  • When you add an embedded resource, the data is stored directly in the project's resource (.resx) file. Strings can only be stored as embedded resources.

For more information, see Linked and Embedded Resources.

Editing Resources

The Resource Designer enables you to add and modify project resources during development by associating a default application for editing each resource. You access the Resource Designer by right-clicking Properties in Solution Explorer, clicking Open, and then clicking the Resources tab in Project Designer. For more information, see Resources Page, Project Designer. The following illustration shows the Resource Designer menu options:

Resource Designer Menu Items

To edit embedded resources, you must work directly in the .resx file to manipulate the individual characters or bytes. That is why it is more convenient to store complex file types as linked resources during development. You can use the Binary Editor to modify resource files, including the .resx file, at the binary level in either hexadecimal or ASCII format. You can use the Image Editor to modify icons, cursors .jpeg f iles, and GIF files that are stored as linked resources. You can also select other applications as editors for these file types. For more information, see Viewing and Editing Resources in a Resource Editor.

Note

The Binary Editor and Image Editor are available in all editions of Visual Studio except Express editions.

Compiling Resources into Assemblies

When you build your application, Visual Studio invokes the resgen.exe tool to convert your application resources into an internal class named Resources. This class is in the Resources.Designer.cs file which is nested under the Resources.resx file in Solution Explorer. The Resources class encapsulates all your project resources into static readonly get properties as a way to provide strongly-typed resources at run-time. When you build through the Visual C# IDE, all the encapsulated resource data, which includes both the resources that were embedded into the .resx file and the linked files, is compiled directly into the application assembly (the .exe or .dll file). In other words, the Visual C# IDE always uses the /resource compiler option. If you build at a command prompt, you can specify the /linkresource compiler option that will enable you to deploy resources in a separate file from the main application assembly. This is an advanced scenario and is only necessary in certain rare situations. A more common scenario for deploying resources separately from the main application assembly is to use satellite assemblies as discussed in a later section.

Accessing Resources at Run-Time

To access a resource at run-time, reference it as you would any other class member. The following example shows how to retrieve a bitmap resource that you named Image01.

System.Drawing.Bitmap bitmap1 = Properties.Resources.Image01;

Internally the get property uses the ResourceManager class to create a new instance of the object.

For more information, see Resource File Generator (Resgen.exe).

Resources in Satellite Assemblies

If you are creating applications that will be localized (translated) into multiple languages, you can store each set of culture-specific strings as a resource in its own satellite assembly. When you distribute your application, you include the main application assembly and all appropriate satellite assemblies. You can then add satellite assemblies or modify existing ones without recompiling the main application assembly. For more information about satellite assemblies online on MSDN, search for "Creating Satellite Assemblies" and "Locating and Using Resources for a Specific Culture".

See Also

Other Resources

Using the Visual C# Express IDE