gpt4 book ai didi

c# - 无法将自定义配置部分保存到 app.config

转载 作者:行者123 更新时间:2023-11-30 12:51:34 25 4
gpt4 key购买 nike

我正在努力将自定义部分保存到我的 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//下。请检查那个位置

关于c# - 无法将自定义配置部分保存到 app.config,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6695329/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com