Live Chat Icon For mobile
Live Chat Icon

WinForms FAQ - Resources

Find answers for the most frequently asked questions
Expand All Collapse All

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

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

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

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

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
//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

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.