read string from .resx file in C#

前端 未结 14 2099
一生所求
一生所求 2020-12-02 06:01

How to read the string from .resx file in c#? please send me guidelines . step by step

相关标签:
14条回答
  • 2020-12-02 06:39

    This works for me. say you have a strings.resx file with string ok in it. to read it

    String varOk = My.Resources.strings.ok
    
    0 讨论(0)
  • 2020-12-02 06:42

    Assuming the .resx file was added using Visual Studio under the project properties, there is an easier and less error prone way to access the string.

    1. Expanding the .resx file in the Solution Explorer should show a .Designer.cs file.
    2. When opened, the .Designer.cs file has a Properties namespace and an internal class. For this example assume the class is named Resources.
    3. Accessing the string is then as easy as:

      var resourceManager = JoshCodes.Core.Testing.Unit.Properties.Resources.ResourceManager;
      var exampleXmlString = resourceManager.GetString("exampleXml");
      
    4. Replace JoshCodes.Core.Testing.Unit with the project's default namespace.

    5. Replace "exampleXml" with the name of your string resource.
    0 讨论(0)
  • 2020-12-02 06:42

    Create a resource manager to retrieve resources.

    ResourceManager rm = new ResourceManager("param1",Assembly.GetExecutingAssembly());
    
    String str = rm.GetString("param2");
    

    param1 = "AssemblyName.ResourceFolderName.ResourceFileName"

    param2 = name of the string to be retrieved from the resource file

    0 讨论(0)
  • 2020-12-02 06:43

    I added my resource file to my project directly, and so I was able to access the strings inside just fine with the resx file name.

    Example: in Resource1.resx, key "resourceKey" -> string "dataString". To get the string "dataString", I just put Resource1.resourceKey.

    There may be reasons not to do this that I don't know about, but it worked for me.

    0 讨论(0)
  • 2020-12-02 06:47

    ResourceManager shouldn't be needed unless you're loading from an external resource.
    For most things, say you've created a project (DLL, WinForms, whatever) you just use the project namespace, "Resources" and the resource identifier. eg:

    Assuming a project namespace: UberSoft.WidgetPro

    And your resx contains:

    resx content example

    You can just use:

    Ubersoft.WidgetPro.Properties.Resources.RESPONSE_SEARCH_WILFRED
    
    0 讨论(0)
  • 2020-12-02 06:48

    The Simplest Way to get value from resource file. Add Resource file in the project. Now get the string where you want to add like in my case it was text block(SilverLight). No need to add any namespace also.Its working fine in my case

    txtStatus.Text = Constants.RefractionUpdateMessage;
    

    Constants is my resource file name in the project.

    0 讨论(0)
提交回复
热议问题