无法将自定义配置部分保存到app.config

时间:2011-07-14 14:59:20

标签: c# configuration-files

我很难将自定义部分保存到app.config中。这是我到目前为止的代码:

            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);

问题是配置文件未被修改。如果我遍历TunnelSection,我会看到已添加新主机。

为了排除权限问题,我尝试将设置添加到这样的appsetting:

configuration.AppSettings.Settings.Add("hola", "hola");

这很好。

我也尝试过saveas,但无济于事。

有什么想法吗? TIA

1 个答案:

答案 0 :(得分:1)

无法修改系统设置,但可以进行用户设置。用户设置不保存在应用程序目录中,而是保存在/ appData //下。请检查该位置