Windows Forms Resources
FAQ Home
1.
What are the steps to compiling and using multiple language resources?
2. How do I load a BMP file that has been added to my solution
as an embedded resource?
3. How can I save a temporary disk file from an embedded
string resource?
4. How do I embed a manifest file into my exe?
5. How do I read or write resources from code?
6. How do I create resources to make culture-aware programs
without re-compiling code?
7. How do I programmatically use resources to make my programs culturely aware?
8. How can I dynamically load a resource file?
9. How do I read embedded resources?
10. How can I check if a certain resource is included in a
assembly?
11. Where can I find information on globalization?
1 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.
2 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 Exisitng Item...
menu item, then change the Build Action property of this BMP file to
Embedded Resource, you can then access this resoource with code similar to:
string bmpName =
"WindowsApplication6.sync.bmp"; // WindowsApplication6 corresponds to
Default Namespace in your project settings.
System.IO.Stream strm = null;
try
{
strm = this.GetType().Assembly.GetManifestResourceStream(bmpName);
pictureBox1.Image = new Bitmap(strm);
}
catch(Exception e)
{
MessageBox.Show(e.Message);
}
finally
{
if(strm != null)
strm.Close();
}
3 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;
}
4 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.
5 How do I read or write resources from code?
You can use the ResourceWriter object to create resource files.
Take a look at the discussion and code found on
gotdotnet.com
6 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.
Take a look at the discussion and code found on
gotdotnet.com
7 How do I programmatically use resources to make my
programs culturely 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.
Take a look at the discussion and code found on
gotdotnet.com
8 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.
9 How do I read embedded resources?
Check out the entry titled
How do I
load a BMP file that has been added to my solution as an embedded resource?
in this section. That code can be used for any kind of embedded resource.
10 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.
11 Where can I find information on globalization?
Here are some MSDN globalization samples:
Visual Basic .NET Code Sample: Working with Resource Files
.NET Samples - How To: Globalization and NLS
Also look at the following topic in online help inside IDE.
Developing World-Ready Applications
|