Reputation: 103
I'm struggling to save a custom section to my app.config. This is the code I have so far:
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Read the TunnelSection from the configuration file
TunnelSection tunnels = ConfigurationManager.GetSection("TunnelSection") as TunnelSection;
HostConfigElement newhost = new HostConfigElement();
newhost.Password = "hola";
newhost.SSHPort = 22;
newhost.SSHServerHostname = "holahola";
TunnelConfigElement newtunnel = new TunnelConfigElement();
newtunnel.LocalPort = 12;
newtunnel.RemotePort = 12;
newtunnel.Name = "12";
newtunnel.DestinationServer = "12";
TunnelCollection newtunnelcollection = new TunnelCollection();
newtunnelcollection.Add(newtunnel);
newhost.Tunnels = newtunnelcollection;
tunnels.Tunnels.Add(newhost);
ConfigurationManager.RefreshSection("TunnelSection");
configuration.Save(ConfigurationSaveMode.Full);
The issue is that the config file is not being modified. If I loop through the TunnelSection, I see that the new host has been added.
To rule out permissions issues, I tried adding a setting to appsettings like this:
configuration.AppSettings.Settings.Add("hola", "hola");
and this works fine.
I also tried saveas but to no avail.
Any ideas? TIA
Upvotes: 5
Views: 1884
Reputation: 6999
System Settings cannot be modified but user settings can be. User settings are not saved on application directory but saved under /appData//. Please check at that location
Upvotes: 1