5

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

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
mystery
  • 153
  • 1
  • 1
  • 14

1 Answers1

10

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();
Stephan Zaria
  • 496
  • 5
  • 12
  • Thanks for your reply. Actually i need just one string inside the program and i want to change in one form. How can i do that? With writing reading and editing the configuration file, is it possible? – mystery Dec 25 '13 at 13:26
  • Why do you need to change it? Is it some kind of user setting? If so, then the configuration file is not only possible, but the most logical way to do it. – Stephan Zaria Dec 25 '13 at 13:36
  • Yes something like that. For my program i give a default value but user can change it. How can i use configuration file for this purpose? CAn you give me an example of that? – mystery Dec 25 '13 at 13:55
  • 1
    I've added a detailed explanation to my answer. Please re-read it. And you should still check the supplied link out - it can give you some good info on the subject. – Stephan Zaria Dec 25 '13 at 14:08
  • Thanks i have checked the link and your detailed answer. I think it works for me – mystery Dec 25 '13 at 14:18