C# winform把新值写入app.config文件

最简单的winform配置文件app.config如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ftpServer" value="192.168.123.456" />
    <add key="ftpUserName" value="hahaha" />
    <add key="password" value="123456" />
    <add key="localDefaultDirectory" value="E:/" />
    <add key="remoteDefaultDirectory" value="/" />
    <add key="nameList" value="123  456 789 10 11 12" />
  </appSettings>
</configuration>

网上找的只能暂时把新设置的值加载进去,典型处理方法如下:

private void AccessAppSettings()
{
    //获取Configuration对象
    Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    //根据Key读取<add>元素的Value
    string name = config.AppSettings.Settings["name"].Value;
    //写入<add>元素的Value
    config.AppSettings.Settings["name"].Value = "fx163";
    //增加<add>元素
    config.AppSettings.Settings.Add("url", "http://www.fx163.net");
    //删除<add>元素
    config.AppSettings.Settings.Remove("name");
    //一定要记得保存,写不带参数的config.Save()也可以
    config.Save(ConfigurationSaveMode.Modified);
    //刷新,否则程序读取的还是之前的值(可能已装入内存)
    System.Configuration.ConfigurationManager.RefreshSection("appSettings");
}

但是回头去看app.config和exe.config,这两个xml文件都没变。这里提供一个直接把新值写入xml的方法:

private void butOk_Click(object sender, EventArgs e)
        {
            XmlDocument doc01 = new XmlDocument();
            XmlDocument doc02 = new XmlDocument();
            //获得配置文件的全路径  
            string  strFileName01 = AppDomain.CurrentDomain.BaseDirectory + "Winftp.exe.config";
            string path = AppDomain.CurrentDomain.BaseDirectory;
            DirectoryInfo dir = Directory.GetParent(path);
            string strFileName02 = dir.Parent.Parent.FullName + "\\app.config";
            doc01.Load(strFileName01);
            doc02.Load(strFileName02);//'F:\work\Win7FTP\Win7FTP\Win7FTP\bin\Debugapp.config'.
            //找出名称为“add”的所有元素  
            XmlNodeList nodes01 = doc01.GetElementsByTagName("add");
            XmlNodeList nodes02 = doc02.GetElementsByTagName("add");
            for (int i = 0; i < nodes01.Count; i++)
            {
                //获得将当前元素的key属性  
                XmlAttribute att = nodes01[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素  
                if (att.Value == "ftpServer")
                {
                    //对目标元素中的第二个属性赋值  
                    att = nodes01[i].Attributes["value"];
                    att.Value = this.txtHostName.Text.ToString();
                    break;
                }
            }
            for (int i = 0; i < nodes02.Count; i++)
            {
                //获得将当前元素的key属性  
                XmlAttribute att = nodes02[i].Attributes["key"];
                //根据元素的第一个属性来判断当前的元素是不是目标元素  
                if (att.Value == "ftpServer")
                {
                    //对目标元素中的第二个属性赋值  
                    att = nodes02[i].Attributes["value"];
                    att.Value = this.txtHostName.Text.ToString();
                    break;
                }
            }

            //保存上面的修改  
            doc01.Save(strFileName01);
            doc01.Save(strFileName02);
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");


        }
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值