0

As described in the title im having difficulties changing the Values of a NameValueCollection taken from a custom user.config . For some reason whenever i try to change a value with either SET-method or ADD/REMOVE-method it throws me an exception and informs me that the collection is read-only I've also tried it with the app.config but its the same result, however if i edit it as XML ELement and then via ChildNodes then it works. Any help is much appreciated. Here is my Code:

user.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="users_fächer" type="System.Configuration.NameValueSectionHandler" />
  </configSections>
  <users_faecher>
    <add key="user1" value="value1" />
    <add key="user2" value="value2" />
  </users_faecher>
</configuration>

Implementation:

string dir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Users.config");
string new_fächer = input[1];
ExeConfigurationFileMap configMap = new ExeConfigurationFileMap();
configMap.ExeConfigFilename = dir;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configMap, ConfigurationUserLevel.None);

ConfigurationSection myParamsSection = config.GetSection("users_faecher");

string myParamsSectionRawXml = myParamsSection.SectionInformation.GetRawXml();
XmlDocument sectionXmlDoc = new XmlDocument();
sectionXmlDoc.Load(new StringReader(myParamsSectionRawXml));
NameValueSectionHandler handler = new NameValueSectionHandler();

NameValueCollection users_fächer = handler.Create(null, null, sectionXmlDoc.DocumentElement) as NameValueCollection;

users_fächer.Set(user, new_fächer);
config.Save(ConfigurationSaveMode.Modified, true);
  • `NameValueSectionHandler.Create()` creates an object, but I don't see a cast in your code. How are you doing the cast? – vipersassassin Mar 15 '17 at 14:30
  • why are you trying to alter your config at run time? – Alexander Matusiak Mar 15 '17 at 14:35
  • @vipersassassin I'm not doing any cast prior to what you see in the code atm. Could you give me a short example of how it should look like? –  Mar 15 '17 at 14:40
  • http://stackoverflow.com/questions/758389/why-are-application-settings-read-only-in-app-config – Trey Mar 15 '17 at 14:40
  • @ALexanderMatusiak because i use it as a storage for user information that the user should be able to edit so that at the next runtime the updated information is still visible and gets initialized at the start. an example would be the configuration of checkboxes –  Mar 15 '17 at 14:41
  • @Trey I've seen that post already and im not using app.config atm, idk why it still doesnt work or have i misunderstood the comment in that post? –  Mar 15 '17 at 14:44
  • If you want to persist user information, use a different mechanism. Something as simple as a csv data file will be easier to work with. Configuration should be data used to configure the application per environment, not user data. The code you posted is way more complex than opening a stream reader to a text file, and parsing a collection in - and then writing the collection back out. – Alexander Matusiak Mar 15 '17 at 15:04
  • @AlexanderMatusiak while that is probably true (but also not as tidy), it's possible to somehow edit the anything in AppSettings> in any config but for some reason that does not apply to the sections ive created. It must be possible to edit it that way as its working with that aswell as XML Element. –  Mar 15 '17 at 15:11
  • From a design perspective, I think mixing per-user configuration data into your application configuration to be far more confusing. Just because you can do it, doesn't mean it is the best way to accomplish what you need to do. – Alexander Matusiak Mar 15 '17 at 15:16
  • @AlexanderMatusiak that true ;D, well if no possible way is found in this post then i will just continue using XML-Element as then Sections can be added aswell and its not that too bad to use. For the log run i will probably look out for a better way of storing user data though. –  Mar 15 '17 at 15:20

0 Answers0