Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am trying to some keys in my Windows Application dynamically at the run time. I am able to change the existing key's value at the runtime. Can anyone please help me on how to create Key dynamically at runtime?

Thanks,
Posted

You can't (not ones that show up as properties of the Settings class). The settings class with the properties is generated at compile-time, so adding properties would not regenerate that class or your executable. In order to do that you may want to look into different methods of saving settings.

Since you can't serialize the standard dictionary, or hash table, you could roll your own serializable version and save them to an XML file, or you can skip the dictionary all together and create your own Key/Value pair, store it in a List and serialize that. You can then transform the list into a dictionary later for easier access...

Or use a database, etc. There are a number of ways to do it, unfortunately using the Application Settings is not one of them.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jul-13 11:26am    
Agree, a 5. You could just say that config files are not designed to be modified during runtime. The configuration is just loaded and used. This is just a way to have some predefined configuration (modified, say, by an installer) which can be manually modified by a user, to get some different behavior.
—SA
adityasahver 23-Jul-13 3:25am    
At the Runtime, i can update the values for the key in the app.config then why cant we add a key in the app.config. Is there much difference in both the tasks, an explanation(with example) will be appreciated.
Thanks,
Sergey Alexandrovich Kryukov 23-Jul-13 10:13am    
I just doubt it would make any sense.
—SA
adityasahver 23-Jul-13 6:55am    
Ron, i just found the way to update and add keys in the app.config dynamically.
Thanks
Sergey Alexandrovich Kryukov 23-Jul-13 10:13am    
Really? I would question: why?
—SA
You only can use generic types in application settings.

Try one of these:

C#
System.Collections.Specialized.StringDictionary
or
System.Collections.Specialized.StringCollections


Maybe this is helpful too:
store-dictionarystring-string-in-application-settings[^]
 
Share this answer
 
Hi,
As i said, that i am able to update the app.config key value pair from the code.
Below is the code which i just updated. The code worked for me, and solution to the question is as well-

C#
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
          if (configuration.AppSettings.Settings[key] != null)
          {
              configuration.AppSettings.Settings.Add("key","value");
              //[key].Value = value;

          }

          configuration.Save();

          ConfigurationManager.RefreshSection("appSettings");


Thanks for the inputs provided.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900