473,473 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How To Use ResourceManger for String management

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));
}
}
}

Dec 14 '06 #1
3 3338
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.

<gr**********@gmail.comwrote in message
news:11**********************@l12g2000cwl.googlegr oups.com...
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));
}
}
}

Dec 15 '06 #2
Thanks a bunch Kevin, I was missing the part about
"Properties.Resources.FooString" and now understand how it works.
Kevin Spencer wrote:
The simplest way in VS 2005 is to right-click the project in Solution
Explorer and select the "Settings" or "Resources" tab.
Dec 15 '06 #3
Thank you Kevin, I was missing the part about
"Properties.Resources.ReportToolTipText"

Dec 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Ellarco | last post by:
Im sorry for asking a question that is surely in the archives somewhere, but I have been unable to locate it. Its about string memory management. I need to dynamically construct a C-style string...
3
by: Scott Brady Drummonds | last post by:
Hi, all, I've a fairly small piece of code that is causing me problems. I'm using std::string and am building a string of several dozen characters using several of std::string's functions: a...
2
by: Bob Dankert | last post by:
I need to use functions through PInvoke which accept and return pointers to strings. What is the best way to get string pointers (IntPtr I am guessing?) to pass into these functions, and the best...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
1
by: muntyanu | last post by:
Hi all, I am passing string from C++ to C# but not sure which way is more correct. Here it is: First approach: CDAnetCSharpHooks::PassString( ) { char str = "String to pass"; DotNetObject...
94
by: smnoff | last post by:
I have searched the internet for malloc and dynamic malloc; however, I still don't know or readily see what is general way to allocate memory to char * variable that I want to assign the substring...
4
by: =?Utf-8?B?Z3V5?= | last post by:
Is this a valid approach to State Management? What I do is define a class 'State' which holds all the objects etc. that I need to store in the Session. I can then get my state object and...
2
by: Johnson | last post by:
I'm trying to fix a "sub optimal" situation with respect to connection string management. Your thoughtful responses will be appreciated. I just started with a new client who has a bunch of legacy...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.