app.config保存设置的时候 怎么不变

app.config保存设置的时候 怎么不变

你用的哪个办法?xml的方式还是System.Configuration?System.Configuration文章说了改不了。而且注意改的app.config的话,需要重新编译过才会更新debug目录下的相关config文件,app.config是不拷贝到编译后的debug目录下的,除非人工拷贝。题主是不是理解成修改app.config后,自动修改debug目录下的配置文件了?

最好发你的代码来看下~,下面代码测试可以正常修改app.config,有帮助麻烦点个采纳【本回答右上角】,谢谢~~


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
using System.Linq;
using System.Xml.Linq;
namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var config = @"F:\CSharp\WindowsForms\WindowsFormsApp1\app.config";
            XDocument doc = XDocument.Load(config);
            var keys = new[] { "TempValue", "WetValue" };
            var nodes = doc.Descendants().Where(i =>i.Name.LocalName=="add"&& keys.Contains(i.Attribute("key").Value)).ToList();
            nodes[0].Attribute("value").Value = textBox1.Text;
            nodes[1].Attribute("value").Value = textBox2.Text;

            MessageBox.Show(doc.ToString());
            doc.Save(config);
        }
    }
}


一般配置文件不生效的时候记得重新编译一下在运行(不是F5启动),然后才重新运行就可以了