c# – 无法将自定义配置部分保存到app.config
发布时间:2020-12-15 21:20:52 所属栏目:百科 来源:网络整理
导读:我正在努力将自定义部分保存到我的app.config中.这是我到目前为止的代码: Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //Read the TunnelSection from the configuration file TunnelSection
我正在努力将自定义部分保存到我的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但无济于事. 有任何想法吗? 解决方法
系统设置无法修改,但用户设置可以.用户设置不保存在应用程序目录中,而是保存在/ appData //下.请检查该位置
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |