Resources in C#

G

Greg

Can someone help me using resources in C#/.NET 2005?

I've found numerous examples on the internet. They all seem to refer to
resources files ending in ".resource". why? When I add a new resource
file, the filetypes are ".resx". Or some others want me to use resgen..
I'd rather stay out of the command prompt, and I'm sure it must be
possible to do everything through the IDE.

I'd basically like to have "StringResource.resx" and
"StringResource.ca-FR.resx", and use these from within my code. Right
now, with these in my Properties folders, and using the following code,
it can't find the resource.

ResourceManager resource = new ResourceManager("StringResource",
Assembly.GetExecutingAssembly());

Thanks,
Greg
 
K

Kevin Spencer

Right-Click the Project and select Properties. Go to the Resources tab. Add
a file resource. Then you can access it in the app via

Properties.Resources.Resourcename

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
G

Greg

Thanks! Do I need to do this for each file/locale, or just for the base
file?

Greg

-----Original Message-----
From: Kevin Spencer [mailto:[email protected]]
Posted At: January 16, 2008 10:42 AM
Posted To: microsoft.public.dotnet.framework.windowsforms
Conversation: Resources in C#
Subject: Re: Resources in C#


Right-Click the Project and select Properties. Go to the Resources tab.
Add a file resource. Then you can access it in the app via

Properties.Resources.Resourcename

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP
 
S

Steven Cheng[MSFT]

Hi Greg,

The way Kevin mentioned can help you add resource into the project's main
resource file. You can also add your own resx file into project and read
resource from them in code. Just like what you do in your first reply:

** add a new resx file into project

** add resource items into the resx file

** use ResourceManager to retrieve resource items from it.

The problem that you didn't get anything is due to the following reason:

For .NET C# project, VS project will use project name as its "default
namespace", therefore, if you add a certain resx file into the project,
suppose "StringResource.resx" and the project name is
"WindowsFormsApplication1", the full name of the resource file is

"WindowsFormsApplication1.StringResource" rather than "StringResource"

e.g.

====================

private void button1_Click(object sender, EventArgs e)
{
ResourceManager rm = new
ResourceManager("WindowsFormsApplication1.StringResource",
typeof(Form1).Assembly);
string str1 =rm.GetString("String1");
MessageBox.Show(str1);
}
=====================

You can try it on your side to see whether it works.

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================


This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
Subject: Re: Resources in C#
Date: Wed, 16 Jan 2008 16:08:58 -0500
Thanks! Do I need to do this for each file/locale, or just for the base
file?

Greg

-----Original Message-----
From: Kevin Spencer [mailto:[email protected]]
Posted At: January 16, 2008 10:42 AM
Posted To: microsoft.public.dotnet.framework.windowsforms
Conversation: Resources in C#
Subject: Re: Resources in C#


Right-Click the Project and select Properties. Go to the Resources tab.
Add a file resource. Then you can access it in the app via

Properties.Resources.Resourcename

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

Greg said:
Can someone help me using resources in C#/.NET 2005?

I've found numerous examples on the internet. They all seem to refer
to resources files ending in ".resource". why? When I add a new
resource file, the filetypes are ".resx". Or some others want me to use resgen..
I'd rather stay out of the command prompt, and I'm sure it must be
possible to do everything through the IDE.

I'd basically like to have "StringResource.resx" and
"StringResource.ca-FR.resx", and use these from within my code. Right
now, with these in my Properties folders, and using the following
code, it can't find the resource.

ResourceManager resource = new ResourceManager("StringResource",
Assembly.GetExecutingAssembly());

Thanks,
Greg
 
K

Kevin Spencer

Hi Greg,

Sorry, I didn't see that you're using Localization. Visual Studio's Resource
Designer can be used for localized resources as well. The following article
explains how:

http://technet.microsoft.com/en-us/library/aa992030.aspx

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

Greg said:
Thanks! Do I need to do this for each file/locale, or just for the base
file?

Greg

-----Original Message-----
From: Kevin Spencer [mailto:[email protected]]
Posted At: January 16, 2008 10:42 AM
Posted To: microsoft.public.dotnet.framework.windowsforms
Conversation: Resources in C#
Subject: Re: Resources in C#


Right-Click the Project and select Properties. Go to the Resources tab.
Add a file resource. Then you can access it in the app via

Properties.Resources.Resourcename

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

Greg said:
Can someone help me using resources in C#/.NET 2005?

I've found numerous examples on the internet. They all seem to refer
to resources files ending in ".resource". why? When I add a new
resource file, the filetypes are ".resx". Or some others want me to use resgen..
I'd rather stay out of the command prompt, and I'm sure it must be
possible to do everything through the IDE.

I'd basically like to have "StringResource.resx" and
"StringResource.ca-FR.resx", and use these from within my code. Right
now, with these in my Properties folders, and using the following
code, it can't find the resource.

ResourceManager resource = new ResourceManager("StringResource",
Assembly.GetExecutingAssembly());

Thanks,
Greg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top