Deeptechtons
Deeptechtons

Reputation: 11125

C# - Fastest way to get resource string from assembly

I really don't know/have the answer, knowledge to find a resource value using a key from a resx file in a assembly using c#.(or may be i am ignorant).

What would be the fastest code, way i can retrieve string values or values using a key from a resource file which is embedded as resource in a assembly. I am storing friendly messages for exceptions in the resource file and would like to use them when required.

Does a static class exist for this purpose?

Are there open source mature projects i can use for this?

Upvotes: 28

Views: 86197

Answers (7)

Chris
Chris

Reputation: 1146

To improve on Herzbube's answer I will show how I implemented this...

Rather than creating projects or folders for the resource file, just right click your project and do add -> new item, then choose resources file. Open the resources file stick in your strings, save as a useful name, and navigate over to C# where you want to use them, then it is just:

String resourceValue = MyProjectName.MyResourceFileName.MyResourceRowItem;

If that isnt working pay attention to the access modifier drop down when inside your resx file.

Upvotes: 3

Anup Shetty
Anup Shetty

Reputation: 571

 var thread = System.Threading.Thread.CurrentThread.CurrentUICulture.Name;
            var culture = new CultureInfo(thread);
            var resourceManager = new ResourceManager(typeof(Resources.Resource));
            string value = resourceManager.GetString(name, culture);

Upvotes: 4

SurenSaluka
SurenSaluka

Reputation: 1591

string val = Resources.ResourceManager.GetString("resource_name");

Given "resource_name" you can retrieve resource value.

Upvotes: 16

Robert Snyder
Robert Snyder

Reputation: 2409

When I made a new project for my unit tests of type C# class library called UnitTests, I right clicked and Added a new Resource. I named that UnitTestsResources. I added 2 strings to that resource. I was then able to conveniently able to access them like this

UnitTestsResources.NoDeviceRequireMsg

I was curious how that worked so i pulled up the code behind the resource file and it makes sense. Visual Studio made a internal class with static accessors.. It looks like this for me

[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class UnitTestsResources {

    //Some auto generated code

    /// <summary>
    ///   Looks up a localized string similar to OPOS Device is required for test.
    /// </summary>
    internal static string DeviceRequireMsg {
        get {
            return ResourceManager.GetString("DeviceRequireMsg", resourceCulture);
        }
    }

    /// <summary>
    ///   Looks up a localized string similar to OPOS Device must not be installed for test.
    /// </summary>
    internal static string NoDeviceRequireMsg {
        get {
            return ResourceManager.GetString("NoDeviceRequireMsg", resourceCulture);
        }
    }
}

Since it is only for my unit tests I am content with this. Hope it helps someone else.

Upvotes: 3

herzbube
herzbube

Reputation: 13378

If the resource is in the same assembly as the code, then the following will do:

String resourceValue = MyAssemblyNameSpace.Properties.Resources.ResourceName

Taken from this SO answer.

Upvotes: 48

Ravi Gadag
Ravi Gadag

Reputation: 15881

you can use ResourceManger to get the string value from Assembly

Get Resource from Assembly

ResourceManager ResManager= new ResourceManager("yourResource", 
            Assembly.GetExecutingAssembly());
String strResourveValue = ResManager.GetString("YourStringKey");

Upvotes: 4

sblom
sblom

Reputation: 27363

Assembly assembly = this.GetType().Assembly;
ResourceManager resourceManager = new ResourceManager("Resources.Strings", assembly);
string myString = resourceManager.GetString("value");

Upvotes: 20

Related Questions