Web Design that taps into the haromny and vision of your dreams.

Using Resource Files in C# Applications

A look at resource files in C# and how they are used

Written By on in C#

416 words, estimated reading time 2 minutes.

Resource files are primarily used to hold files associated with the application, such as images, sounds and other data objects. Resource files can also be used to localise applications to specific cultures.

Using Visual Studio Series
  1. Getting Started with Visual Studio
  2. Using the Visual Studio Integrated Development Environment
  3. Using the Visual Studio Debugger
  4. XML Documentation in Visual Studio
  5. Using Microsoft .Net Command Line Tools
  6. What is an Assembly in Microsoft .Net?
  7. Creating and Using a .NET Assembly in C#
  8. Creating and Implementing Satellite Assemblies
  9. Creating Strong Named Assemblies in Microsoft .Net
  10. Visual Studio Task List
  11. Resources and Localisation in C# Projects
  12. Localising Microsoft .Net Applications with C#
  13. Using Resource Files in C# Applications
  14. Using XSD Tool to Generate Classes from XML
  15. 10 Visual Studio Tools You May Not Know About

As well as culture specific applications, resource files can also be used to create a branded or more specialised instance of an application. For example, you can create a generalised application and use resource files and satellite assemblies to create custom branded or specific applications without recompiling the application.

Resources can be added to a project from the "Add new item" menu. Select Resources File and give it a meaningful name. You can open the resource file from the solution explorer which will open the resource editor. From here you can add string resources, images, icons, audio, general files and other documents. Resource files are simple key/value pair combined with a comment and stored as XML.

In this example, I am going to create a simple hello world application loading the text from the resource file. Create a new console application and add a new resource file called applicationResources.resx. In the strings section of the newly created file create a key called "Hello" with a value of "Hello World".

Resource files are strongly typed meaning that we can programmatically reference the keys by name, rather than hard coding the name in a string. To access the Hello World string we can use the following code:

using System;
using System.Text;
 
class Program
{
  static void Main(string[] args)
  {
    Console.WriteLine(applicationResources.Hello);
  }
}

There are two main advantages to using strongly typed resource names. Firstly it's easier to let IntelliSense build up the reference, and secondly, should you change the key name, the application will not compile - you know exactly where to update the code. If you hard coded the name you would have to search and replace followed by testing to make sure that every instance of the incorrect key name was fixed.

Resources can hold many different types, including Bitmap images. In the resource editor, you can change the type of resource from the drop-down list. The strongly typed resources will return an object of the correct class when you access the key. When you access a string it will return a string object and when you access an image it will return a Bitmap object.

Bitmap bmp = applicationStringResources.MyLogo;

Assemblies can be individually compiled into DLL satellite assemblies which can be dropped into the application directory or global assembly cache so they can be used at runtime using the ResourceManager. The process of creating and using satellite assemblies is described in more detail in the Creating and Implementing Satellite Assemblies tutorial.

Last updated on: Thursday 22nd June 2017

 

Comments
Ekrem SABAN

Ekrem SABAN

Hello!

I am asking about a bit more complicated case, where the "Hello, World!" program can also greet the World in German.

I modified your code to
static void Main(string[] args)
{
Console.Write("0: English, 1: German");
int language = Console.Read();

switch (language)
{
case 1: // de
Console.WriteLine(applicationResourcesDE.Hello);
break;
default: // en
Console.WriteLine(applicationResources.Hello);
break;
}

Console.Read();
}

I also created a second resource with the name above for German that has the same "Hello" label in it. All done in Microsoft Visual Studio 2010 Express...

When I now try to run it, there is a compile-time error which reads

The name "applicationResourcesDE" does not exist in the current context.

So, I am asking about the more complicated case of creating somehow a "current context" that also "knows" about German greeting...

Thank you for your reply!

Reply to Ekrem SABAN
Ekrem SABAN

Ekrem SABAN

Found what was missing: I had to select on the top of the applicationResourceDE in the combo box "Internal" (defaults to "no code generation").

Reply to Ekrem SABAN
Akky

Akky

Hello!
I have a list of strings stored in a file called as "Data.txt" that i have added to the Resources.resx.
Now, i was to compare it with a String stored in the Windows form.
I know that i can access the file using
"Properties.Resources.Data"
but now how can i compare its contents with the sting stored in Windows Application From.
Like i have :-
string s1 = "bus";
and in the Data.txt , I have i list like this....
cat
mat
bus
road
shard
see
help
me
out
people

So, how can i compare this list in Data.txt with "s1" and find out if s1 is a part of Data.txt or not....??
P.S. :- "Please Help"

Reply to Akky

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me