Reading a resource string

  • Thread starter Thread starter Peter Larsen []
  • Start date Start date
Hi Peter,

It depends on how you embed the string as a resource in the assembly.

1. If you write the string in a file, e.g. a .txt file and embed the file
as a resource by setting the "Build Action" property of the file to
Embedded Resource, you could use the Assembly.GetManifestResourceStream
method to get the string. The following is a sample, which assumes that the
.txt file's name is test.txt:

Assembly asm = Assembly.GetExecutingAssembly();
Stream stream = asm.GetManifestResourceStream("projectnamespace.test.txt");
StreamReader streamReader = new StreamReader(stream);
string str = streamReader.ReadLine();

2. If you add the file that contains the string to a .resx file, you can
get the string using ResourceManager.GetObject method. For example, we add
the test.txt file to a .resx file named test2.resx and the corresponding
resource name is "test". The following is the code to get the string
contained in the text.txt file:

Assembly asm = Assembly.GetExecutingAssembly();
ResourceManager rm = new ResourceManager("projectnamespace.test2", asm);
string str = rm.GetObject("test");

3. If you add the string directly to a .resx file and the corresponding
resource name is "String1", you can use the ResourceManager.GetString
method as follows:

Assembly asm = Assembly.GetExecutingAssembly();
rm = new ResourceManager("projectnamespace.test2", asm);
string str = rm.GetString("String1");

Hope this helps.
If you have any question, please feel free to let me know.

Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
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.
 
Hi Linda,

Sorry, i didn't explained this very well.
I'm trying to read a resource string from shell32.dll.

/Peter
 
Hi Linda,

I have found a solution using GetModuleHandleEx(), LoadString() and
FreeLibrary().
Is there an easier way to do this in dotNet ?

/Peter
 
Hi Peter,

Thank you for your reply and sharing your solution to this problem!

I have spent some time researching on this issue. What I found is same as
you did.

Since shell32.dll is an unmanaged dll, we need to use some Win32 API
functions to retrieve the resource string from the dll. I am afraid that
there's no easier way to do this in .NET.

If you have any other question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support
 
Back
Top