?
Solved

How do I retrieve resource string using C#?

Posted on 2005-04-18
4
Medium Priority
?
775 Views
Last Modified: 2012-05-05
I have a myresource.dll in which I have the text strings I need to retrieve.
What kind of declarations and code do I need to retrieve a string from this dll?
I believe I need to use the ResourceManager class and use its GetString method, but I don't know the COMPLETE code (including declarations, etc.) to make this work.

Let's say I have only one resource text string in this dll
Message = Hello.
Please give me the code to retrieve this text resource.
Thanks.

0
Comment
Question by:rj94070
4 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 900 total points
ID: 13804651
// Gets a reference to the same assembly that
// contains the type that is creating the ResourceManager.
System.Reflection.Assembly myAssembly;
MyAssembly = this.GetType().Assembly;

// Gets a reference to a different assembly.
System.Reflection.Assembly myOtherAssembly;
myOtherAssembly = System.Reflection.Assembly.Load("ResourceAssembly");

// Creates the ResourceManager.
System.Resources.ResourceManager myManager = new
   System.Resources.ResourceManager("ResourceNamespace.myResources",
   myAssembly);

System.String myString;
myString = myManager.GetString("StringResource");

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskretrievingresourceswithresourcemanager.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskCreatingResourceAssemblies.asp
0
 
LVL 11

Assisted Solution

by:jatinderalagh
jatinderalagh earned 300 total points
ID: 13805082
CheckOut this article which explains how to add resources and retrive them.

http://www.funducode.com/csharpart/csarticle19.htm
0
 
LVL 22

Assisted Solution

by:_TAD_
_TAD_ earned 300 total points
ID: 13807417
It really depeds on how the data was stored.  If it's in a resource file, then you'd want to use the ResourceManager object.  If it's stored in a .net dll, then you may want to use reflections.  Finally, if your data is stored in a COM object (vb6.dll or C++.dll), you'll have to use the Runtime.Interop namespace in conjunction with the DllImport attribute.

You mentioned that your data is stored in a DLL.  What kind of DLL is it (.Net assembly, or COM object)?

[DllImport("MyComObject.dll")]
public static string GetMyResourceString();

public Button1_ClickEvent()
{
   TextBox1.Text = GetMyResourceString();
}
0
 

Author Comment

by:rj94070
ID: 13809886
AlexFM,
I don't know why I have to do
// Gets a reference to a different assembly.
I have only one resource assembly in my solution.


TAD,
I need the code for GetMyResourceString()

To clarify: I created a .resx file then created a .NET dll. I put this dll in a separate "Output: Class Library" project within a solution. There is no code in this dll other than a .resx .
Thanks.

0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month16 days, 9 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question