Share via


Adding and Editing Resources (Visual C#)

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 right-clicking the Properties node under your project in Solution Explorer, clicking Open, and then 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 to the resource file on disk. If you add images, videos, or other complex files as linked resources, you can edit them 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 and Resources in .Resx File Format.

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 edit resource files, including the .resx file, at the binary level in either hexadecimal or ASCII format. You can use the Image Editor to edit icons and cursors as well as .jpeg and GIF files that are stored as linked resources. You can also choose other applications as editors for these file types. For more information, see Viewing and Editing Resources in a Resource Editor.

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 called Resources. This class is contained 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 of providing strongly-typed resources at run-time. When you build through the Visual C# IDE, all the encapsulated resource data, including 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 from the command line, 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 below.

Accessing Resources at Run-Time

To access a resource at run-time, simply reference it as you would any other class member. The following example shows how to retrieve a bitmap resource that you named Image01. Note that the Resources class is in a namespace called <projectName>.Properties, so you must either user the fully qualified name for each resource or else add the appropriate using directive in the source file from which you are accessing the Resources class.

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

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

For more information, see Resources in Applications and 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 along with any appropriate satellite assemblies. You can then add additional satellite assemblies or modify existing ones without recompiling the main application assembly. For more information, see Creating Satellite Assemblies and Locating and Using Resources for a Specific Culture.

See Also

Concepts

Introduction to the Project Designer

Other Resources

Visual C#

Getting Started with Visual C#

Assemblies in the Common Language Runtime

Globalizing and Localizing Applications