Getting values from my resource after publishing.

Maor kam 21 Reputation points
2022-01-19T04:41:35.857+00:00

Hello.

I want to get a value from my resx file.

In my project, I use 2 approaches to get the value:

1.

ResourceManager resourceManager = new ResourceManager("My_Resource_Path", Assembly.GetExecutingAssembly());
string value = resourceManager.GetString("key");

2.

string value = GetGlobalResourceObject("resource_name", "key").ToString();

I have a resx file. and the property "Build Action" set to Embedded Resource.

My problem is that when I publish my project it doesn't work. (because GetGlobalResourceObject retrieves resources from a global .resx file which is not generated, because my build action property set to embedded resource and not to content.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,936 questions
{count} votes

Accepted answer
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2022-01-21T08:25:48.167+00:00

    @Maor kam , I recommend that you used System.Resources.ResourceReader to read the resource file after publishing.

    You can refer to the following steps to do it.

    First, Please create a folder and add the resource file to the folder in your project.

    167096-image.png

    Second, Please change Build Action to Content.

    167135-image.png

    Third, we could use the following code to access the resource file after publishing.

      private void button1_Click(object sender, EventArgs e)  
            {  
                string paath = AppDomain.CurrentDomain.BaseDirectory + @"Data\ExampleResources.resources";  
                MessageBox.Show(paath);  
                var rr1 = new System.Resources.ResourceReader(paath);  
                foreach (DictionaryEntry entry in rr1)  
                {  
                    if (entry.Key.ToString()=="Greeting")  
                    {  
      
                        textBox1.Text = entry.Value.ToString();   
      
                    }  
      
                }  
            }  
    

    Finally, we can see the following result:

    167172-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.