代码:
               string str = txtMusic.Text;
                //写配置
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings["backMusic"].Value = str;
                //config.Save(ConfigurationSaveMode.Modified);
                //ConfigurationManager.RefreshSection("appSettings");//重新加载新的配置文件 
                config.Save(ConfigurationSaveMode.Modified);配置文件:<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
  <appSettings>
    <add key="backMusic" value=""/>
  </appSettings>
</configuration>

解决方案 »

  1.   

    OpenExeConfiguration不能对配置文件进行修改,换种方法
    http://kb.cnblogs.com/a/1274467/
      

  2.   

    http://hi.baidu.com/591u/blog/item/98c4fcbfa152ee0218d81f1c.html
      

  3.   

    LZ我的博客里面有关于这个的介绍
    传送门还不行+我Q453367672
      

  4.   


    public void Modify(string key, string strValue)                                                                                         //两个参数:要修改的键值   和   要修改的新值;                               
            {
              string   flagstr = strValue;
                if (strValue == string.Empty)
                {
                    MessageBox.Show("连接串不能为空!");
                    return;
                }
                //string XPath = "/configuration/userInfo/add[@key='?']";
                try
                {
                    string XPath = "/configuration/appSettings/add[@key='?']";
                    XmlDocument domWebConfig = new XmlDocument();                //domWebConfig.Load((HttpContext.Current.Server.MapPath("web.config")));
                    domWebConfig.Load(filepath);
                    XmlNode addKey = domWebConfig.SelectSingleNode((XPath.Replace("?", key)));                if (addKey == null)
                    {
                        //Response.Write("<script>alert   (\"没有找到<add   key='" + key + "'   value=.../>的配置节\")</script>");
                        MessageBox.Show("没有找到<add   key='" + key + "'>的配置节");
                        return;
                    }
                    addKey.Attributes["value"].InnerText = strValue;
                    domWebConfig.Save(filepath);
                    MessageBox.Show("数据库连接配置成功","信息提示");
                                }
                catch
                {
                                   return;
                }        }