C# 无法将自定义配置节保存到app.config
我正在努力将自定义部分保存到app.config。这是我目前掌握的代码:C# 无法将自定义配置节保存到app.config,c#,configuration-files,C#,Configuration Files,我正在努力将自定义部分保存到app.config。这是我目前掌握的代码: Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //Read the TunnelSection from the configuration file TunnelSection tun
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//下。请在该位置查看您能澄清appdata在哪里吗?这不是一个web应用程序,也不是以任何方式安装的。到目前为止还只是一个控制台应用,为什么会出现呢?这是一个自定义应用程序,我目前正在使用Visual Studio运行。它没有安装在任何地方。无论如何,我已经做了一个搜索,但它不在那里。也许这个链接能让你更好地理解我想说的。我使用的是嵌套的自定义配置节,因此这不适用