无法将自定义配置节保存到app.config文件中。

5

我正在努力将自定义部分保存到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,但没有成功。

有什么想法吗? 谢谢提供帮助!

1个回答

1

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


你能澄清一下appdata在哪里吗?这不是一个Web应用程序,也没有以任何方式安装。到目前为止,它是一个控制台应用程序。 - yomismo
为什么会在那里呢?这是一个自定义应用程序,我目前正在使用Visual Studio运行。它没有安装在任何地方。 无论如何,我已经搜索过了,它不在那里。 - yomismo
也许这个链接可以让你更好地理解我想说的话。http://msdn.microsoft.com/zh-cn/library/aa730869(v=vs.80).aspx - hungryMind
我正在使用嵌套的自定义配置部分,因此这不适用。 - yomismo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接