Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading string from resource file and editing it programmatically

How can I read string resource file? I have tried this already but I couldn't get the value. For editing it later I couldn't do anything. How can I edit it later programmaticaly? I want to edit its value with a string that I get from textbox.

Assembly assembly = this.GetType().Assembly;
manager = new ResourceManager("StringResources.Strings", assembly);
value = manager.GetString("Name");

For changing its value I tried to do this but it gives me an error does not contain a definition for Current. I try these in windows form.

Application.Current.Resources["Name"] = "abcd";

Please give me an advice Thanks in advance

like image 600
mystery Avatar asked Dec 25 '13 12:12

mystery


People also ask

How do I read a RESX file?

To start with, you need to create a web application. Here, I am using Visual Studio 2012 and C# as the language. Once you created the application, you need to create a RESX file by clicking the “New Item”. Now you can see a new file in your solution explorer named Resource1.

What is Resx in C#?

resx file contains a standard set of header information that describes the format of the resource entries, and specifies the versioning information for the XML code that parses the data. These files contain all the strings, labels, captions, and titles for all text in the three IBM Cognos Office components.

How do I update RESX files?

To edit RESX files, you could use an advanced text editor like XML Editor, but note that simple text editors can corrupt your files. A better way is to use Localazy, crafted with translation file formats in mind.


1 Answers

You cannot edit a resource string. If you want to store some string that you can alter programatically you should use a configuration file, or, even better user or app settings (that are actually a wrapper around the configuration file).

The reason that you can't change a resource string at runtime, is because the resource is compiled into your executable. If you reverse engineer the compiled *.exe or *.dll file, you can actually see your string in the code. Editing an already compiled executable file is never a good idea (unless you're trying to hack it), but when you try to do it from the executables code, it just plain impossible, as the file is locked during execution.

You can read more about user settings on MSDN. You should check out the link, as it contains detailed instructions with screenshots as to how to set your settings through GUI.

In brief, you right click your project->Properties->Settings. Now, you'll see a table where you can add, edit and remove user settings. Once you create a user setting you can use it like this:

//Read
String settingValue = Settings.Default.TestSetting;
//Write
Settings.Default.TestSetting = "newVal";
//Write settings to disk
Settings.Default.Save();
like image 57
Stephan Zaria Avatar answered Nov 02 '22 23:11

Stephan Zaria