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

本文关键字:保存 app config 配置部 自定义 | 更新日期: 2023-09-27 18:02:19

我正在努力保存自定义部分到我的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,就会看到新主机已经添加。

为了排除权限问题,我尝试在应用设置中添加这样的设置:

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

这个可以正常工作

我也试过保存,但是没有用。

任何想法?TIA

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

不能修改系统设置,但可以修改用户设置。用户设置不保存在应用程序目录下,而是保存在/appData//下。