4
我努力将自定义节保存到我的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的位置吗?这不是一个网络应用程序,也没有以任何方式安装。迄今为止的一个控制台应用 – yomismo
你的个人资料夹下的文件夹 – hungryMind
它为什么会在那里?这是一个自定义应用程序,目前我正在使用Visual Studio运行。它没有安装在任何地方。 无论如何我已经做了搜索,它不在那里。 – yomismo