How To Use ResourceManger for String management

  • Thread starter Thread starter groovyjman21
  • Start date Start date
G

groovyjman21

Hi all,

Can someone just give me a dead-simple example of how to find, read,
and use resource files from C# using the VS2005 IDE?

I need to call some strings from a resource file and am having a
hard time understanding how to use ResourceManager. I can find
examples of how to create resource files, but not how to reference them
from C# code. Could somebody please point me to a simple example?
I've tried both using the ResXFile Code generator and tried creating a
resource file from scratch but I keep getting
MissingManifestResourceException. The example below is the best I can
find but it is not clear what the name of the resource file is, where
is it located, and how do I tell VS to include it:

So my question is: using the example below
1)what is the NAME of the resource file expected?
2)where is it placed? How does VS know where it is located?
3) Is there a dead-simple way to do this?



using System;
using System.Globalization;
using System.Reflection;
using System.Resources;

[assembly: NeutralResourcesLanguageAttribute("en-US")]//what does this
do?
//and how are we telling VS what and where our
resourcefiles are?
namespace GlobalizationLibrary
{
public class DoNotPassLiterals
{
ResourceManager stringManager;

public DoNotPassLiterals()
{
stringManager =
new ResourceManager("en-US",
Assembly.GetExecutingAssembly());
}//if I create en-US.resources file in resources directory it
still doesn't find it

public void TimeMethod(int hour, int minute)
{
if(hour < 0 || hour > 23)
{
throw new ArgumentOutOfRangeException(
"hour", hour,
"The valid range is 0 - 23.");
}

if(minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException(
"minute", minute,
stringManager.GetString(
"minuteOutOfRangeMessage",
CultureInfo.CurrentUICulture));
}
}
}
 
The simplest way in VS 2005 is to right-click the project in Solution
Explorer and select the "Settings" or "Resources" tab. If you have no
settings file or Project resources file, it can create one for you. I
mention Settings because you're talking about strings, but you may want the
strings embedded in the exe, in which case you would want to use Resources.
In either case, the Project UI will allow you to easily add strings and
files to project Settings and Resources. It also creates a couple of
namespaces and classes that access Settings and Resources, so that in your
code you can access these as strongly-typed members of those classes.
Example:

private Image OpenNewRecordImage = Properties.Resources.NewRecord;
private Image CloseNewRecordImage = Properties.Resources.CloseNewRecord;

string toolTipText Properties.Resources.ReportToolTipText;

--
HTH,

Kevin Spencer
Microsoft MVP
Bit Player
http://unclechutney.blogspot.com

Expect the unaccepted.

Hi all,

Can someone just give me a dead-simple example of how to find, read,
and use resource files from C# using the VS2005 IDE?

I need to call some strings from a resource file and am having a
hard time understanding how to use ResourceManager. I can find
examples of how to create resource files, but not how to reference them
from C# code. Could somebody please point me to a simple example?
I've tried both using the ResXFile Code generator and tried creating a
resource file from scratch but I keep getting
MissingManifestResourceException. The example below is the best I can
find but it is not clear what the name of the resource file is, where
is it located, and how do I tell VS to include it:

So my question is: using the example below
1)what is the NAME of the resource file expected?
2)where is it placed? How does VS know where it is located?
3) Is there a dead-simple way to do this?



using System;
using System.Globalization;
using System.Reflection;
using System.Resources;

[assembly: NeutralResourcesLanguageAttribute("en-US")]//what does this
do?
//and how are we telling VS what and where our
resourcefiles are?
namespace GlobalizationLibrary
{
public class DoNotPassLiterals
{
ResourceManager stringManager;

public DoNotPassLiterals()
{
stringManager =
new ResourceManager("en-US",
Assembly.GetExecutingAssembly());
}//if I create en-US.resources file in resources directory it
still doesn't find it

public void TimeMethod(int hour, int minute)
{
if(hour < 0 || hour > 23)
{
throw new ArgumentOutOfRangeException(
"hour", hour,
"The valid range is 0 - 23.");
}

if(minute < 0 || minute > 59)
{
throw new ArgumentOutOfRangeException(
"minute", minute,
stringManager.GetString(
"minuteOutOfRangeMessage",
CultureInfo.CurrentUICulture));
}
}
}
 
Thanks a bunch Kevin, I was missing the part about
"Properties.Resources.FooString" and now understand how it works.
 
Thank you Kevin, I was missing the part about
"Properties.Resources.ReportToolTipText"
 
Back
Top