WinForms FAQ - Resources

What are the steps to compiling and using multiple language resources

Localization Sample illustrates how to load different language resources depending upon a parameter passed in when you run the program from a command line. The sample can display English, French, Spanish and German words using the same exe. For example, typing Test de on the command line will count to three in German. Typing Test fr will count to three in French. The argument passed in is used to determine the culture setting for the resources.

In each folder is a batch file that handles compiling the single exe and multiple resources. The tools RESGEN and AL are used in these batch files to generate the resources and resource DLL’s. Notice the naming conventions used by the subfolders containing the foreign resource DLLs.

Permalink
How do I create resources to make culture-aware programs without re-compiling code

Placing culture dependent resources in a separate file from your compiled program code allows you to write applications that can change cultures without having to recompile code to handle different resources.

Permalink
How do I read or write resources from code

You can use the ResourceWriter object to create resource files.

Permalink
How do I programmatically use resources to make my programs culturally aware

You can use the ResourceManager object to dynamically load a particular resource based on a selected culture. This technique allows you to develop culture-aware programs without having to recompile your application.

Permalink
How do I embed a manifest file into my exe

A Win32 resource must be added to your .exe. The type of the resource must be ‘RT_MANIFEST’ and the resource id must be ‘1’. An easy way to do this is with Visual Studio.NET:

  1. Open your exe in VS (file -> open file)
  2. Right click on it and select add resource
  3. Click ‘Import…’ from the dialog
  4. Select your manifest file
  5. In the ‘Resource Type’ field, enter ‘RT_MANIFEST’
  6. In the property grid, change the resource ID from ‘101’ to ‘1’.
  7. Save the exe.

From Mike Harsh at gotnetdot.com.

Permalink
How do I load a BMP file that has been added to my solution as an embedded resource

If you add a BMP file to your solution using the File|Add Existing Item… Menu Item, then change the Build Action property of this BMP file to Embedded Resource, you can then access this resource with code similar to:

	
	// WindowsApplication6 corresponds to Default Namespace in your project settings.
	// subfolders should be the folder names if any, inside which the bmp is added. If the bmp was added to the top level, you don’t have to specify anything here.
	string bmpName = 'WindowsApplication6.subfolders.sync.bmp';  
	System.IO.Stream strm = null;
	try
	{
		strm = this.GetType().Assembly.GetManifestResourceStream(bmpName);
		pictureBox1.Image = new Bitmap(strm);
		// Do the same for Icons 
		// Icon icon1 = new Icon(strm);
	}
	catch(Exception e)
	{
		MessageBox.Show(e.Message);
	}
	finally
	{
		if(strm != null)
			strm.Close();
	}
Permalink
Why doesn’t a Form containing an ImageList not open in WinRes?

There seems to be a bug in the WinRes utility (1.0) that prevents a form from getting loaded in the WinRes editor (usually an exception like ‘Object reference not found’ occurs). A quick workaround is to always name your ImageLists to be of the form ‘imageListN’ (case sensitive; where N is the smallest no. you could use without a clash with other ImageLists).

Permalink
How can I check if a certain resource is included in a assembly?

If a resource is not available in a assembly or in satellite assemblies you will get a message like this ‘Could not find any resources appropriate for the specified culture…’. You can open your assembly and any related satellite assemblies in ILDASM (tool that ships with the .NET framework). When your assembly is open in ILDASM take a look at the manifest file. This contains a list of the resources in your assembly. Usually such errors are the result of incorrect namespaces being used or from spelling errors.

Permalink
How can I save a temporary disk file from an embedded string resource
//usage:   string s = CopyResourceToTempFile(GetType(), 'showcase.txt');
//where showcase.txt is an embedded resource

static string CopyResourceToTempFile(Type type, string name)
{
	string temp = '';
	string nsdot = type.Namespace;
	if (nsdot == null)
		nsdot = '';
	else if (nsdot != '')
		nsdot += '.';
	Stream stream = type.Module.Assembly.GetManifestResourceStream(nsdot + name);
	if (stream != null)
	{
		StreamReader sr = new StreamReader(stream);
		temp = Path.GetTempFileName();
		StreamWriter sw = new StreamWriter(temp, false);
		sw.Write(sr.ReadToEnd());
		sw.Flush();
		sw.Close();
	}
	return temp;
}
Permalink
How can I dynamically load a resource file

You use the ResourceManager class found in System.Resources to access and control resources.

See the article by Caspar Boekhoudt on C# Corner for a detailed discussion.

Permalink