微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

为了排除权限问题,我尝试将设置添加到appsettings,如下所示:

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

这很好用.

我也试过saveas但无济于事.

有任何想法吗?
TIA

解决方法

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐