2011-07-14 33 views
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

回答

1

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

+0

您能澄清appdata的位置吗?这不是一个网络应用程序,也没有以任何方式安装。迄今为止的一个控制台应用 – yomismo

+0

你的个人资料夹下的文件夹 – hungryMind

+0

它为什么会在那里?这是一个自定义应用程序,目前我正在使用Visual Studio运行。它没有安装在任何地方。 无论如何我已经做了搜索,它不在那里。 – yomismo