Simplify Web site maintenance by using resource files in your .NET apps

Resource files provide a location for storing text or objects used across a Web site, thus creating a central location for maintaining the text. Learn how using resource files in your .NET applications can help simplify Web site maintenance.

A resource file contains static, non-executable data that is deployed with an application. A common usage for resource files is error messages and static user interface text. Resource files allow you to easily change the data without touching the application code.

The .NET Framework provides various avenues for working with resource files. This includes comprehensive support for the creation and localization of resources, as well as a simple model for packaging and deploying localized resources. (Localized means it is developed for a certain culture or language, thus you can use resources to provide application access to more than one language.)

More information about resource files

Resources can store data in a variety of formats, including strings, images, and persisted objects. Note that to write persisted objects to a resource file, the objects must be serializable. Also, Microsoft advises against using resource files for storing password or other sensitive data.

Resource files use the .resx file extension. When developing ASP.NET applications via Visual Studio .NET (VS.NET), you will notice a resource file for each Web form used (select the Show All Files icon if they do not appear). Resource files may also be added if you need more, or you are not using Visual Studio.

Like almost all .NET files, resource files are text based and easily edited with your favorite text editor. However, resource files are XML, so valid XML is required. The XML schema is included with resource files created in VS.NET. Listing A shows a simple resource file associated with an ASP.NET Web form.

Weekly .NET tips in your inbox
TechRepublic's free .NET newsletter, delivered each Wednesday, contains useful tips and coding examples on topics such as Web services, ASP.NET, ADO.NET, and Visual Studio .NET.
Automatically sign up today!

A quick review of the XML reveals the XSD comprises almost this entire example. The data element contains our data values. The attributes and elements contained within the data element are defined in the XSD. This includes the following data values:

  • Name: The value used to retrieve the specific data from the resource file. It is analogous to a variable name.
  • Type: The type of data stored.
  • Value: The value assigned to the variable. A quick glance at the XSD reveals this is an option (minOccurs is 0), and it can only have one value (maxOccurs is 1).
  • Comment: Any additional information you want to include with the data.

The value element may contain simple text (as in our example), or it may contain more complex data such as a serialized object or a bitmap image. At this point, we have the data in the resource file, but how is it used in the application? Let's turn our attention to the .NET classes used to access resource data.

Working with resource data

The .NET Framework provides various classes for working with resources in the System.Resources namespace. One good example is the ResourceManager class, which provides access to culture-specific resources. However, if no culture is designated, it falls back to the default culture.

Our example does not have multiple cultures defined, so the default is utilized with the default being the resources attached to the base application DLL. The code in Listing B uses the ResourceManager class to use the data stored in our resource file. (Listing C features the equivalent VB.NET.)

The code runs and retrieves the value from the specific resource file—in this case, it is the resource file associated with the Web form. The text from the data element is displayed in the Web page. This is very simple, but it showcases the flexibility afforded by resource files. You could easily store all user interface text (well, at least the text that is relatively static). This would include toolbar labels, titles, copyright information, and so forth.

As previously stated, you can add new resource files to the project as well. To work with data stored in added resource files, you will use the resource file's name just like TRResource.WebForm1 was used in the previous listing. The following line uses the resource file named Example:

ResourceManagerrm = new ResourceManager("NamespaceName.Example", a);

Here's the equivalent VB.NET code:

Dim rm As ResourceManager
rm = New ResourceManager("NamespaceName.Example", a)

Although this article focuses on ASP.NET, you could use the same approach with a Windows Forms application. A big difference is resource files are not included with Windows applications by default in VS.NET, so you'll have to add resource files to the project.

Application roll-out

If you are developing an application that uses only one language, then pushing it to a Web server is no different than any other project. However, if you are providing localized text via resource files, you'll have to follow a certain approach to ensure .NET can locate the necessary resources for a specific culture/language. This is well beyond the scope of this article, but MSDN provides a wealth of information in this library entry.

More options

Resource files are handy and time-saving when basic site text needs to be changed. You may think that directly editing the text on an ASP.NET page is just as easy, but often text stored in a resource file spans multiple pages. The best application of resource files occurs when providing multiple language versions of a site.

Tony Patton began his professional career as an application developer earning Java, VB, Lotus, and XML certifications to bolster his knowledge.

Miss a column?

Check out the .NET Archive, and catch up on the most recent editions of Tony Patton's column.

About Tony Patton

Tony Patton has worn many hats over his 15+ years in the IT industry while witnessing many technologies come and go. He currently focuses on .NET and Web Development while trying to grasp the many facets of supporting such technologies in a productio...

Editor's Picks

Free Newsletters, In your Inbox