public static void UpdateAppSettings() {
System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Add an entry to appSettings.
int appStgCnt = ConfigurationManager.AppSettings.Count;
string newKey = "NewKey" + appStgCnt.ToString(); string newValue = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString(); config.AppSettings.Settings.Add(newKey, newValue); // Save the configuration file.
config.Save(ConfigurationSaveMode.Modified); // Force a reload of the changed section.
ConfigurationManager.RefreshSection("appSettings"); }上面的只是保存到/debug/appname.exe.config 程序关闭后,这个文件就没有了
我想保存到app.config 文件里面.
有办法吗?

解决方案 »

  1.   

    app.config只是在程序开发期间有效,编译后都是XXX.exe.config了。
    所以你可以重新定义一个xml来放这些信息,或者放到db中。
      

  2.   

    我不清楚 项目里面的app.config文件,在部署后放到哪里了
    要不直接config.SaveAs(path);
      

  3.   


    public static void UpdateAppSettings(string key,string keyValue)//编辑参数
    {
    XmlDocument xmlDoc=new XmlDocument();
    string configPath="appname.exe.config";
    xmlDoc.Load(configPath);
    XmlNode xmlNode=xmlDoc.SelectSingleNode("configuration/appSettings/add[@key='"+key+"']");
    xmlNode.Attributes["value"].InnerText=keyValue;
    xmlDoc.Save(configPath);
    }
    你可以这样保存。
      

  4.   

    string xmlFile = "config.xml";
    string keyName = "Time";
    string valueName = "Value";XmlDocument doc = new XmlDocument();
    if (File.Exists(xmlFile))
    {
        doc.Load(xmlFile);
    }XmlNode nTime = doc.SelectSingleNode(keyName);
    if (nTime == null)
    {
         nTime = doc.CreateElement(keyName);
         doc.AppendChild(nTime);
    }XmlAttribute aTime = nTime.Attributes[valueName];
    if (aTime == null)
    {
        aTime = doc.CreateAttribute(valueName);
        nTime.Attributes.Append(aTime);
    }DateTime now = DateTime.Now;
    aTime.InnerText = string.Format("{0} {1}", now.ToLongDateString(), now.ToLongTimeString());doc.Save(xmlFile);
      

  5.   

    app.config只在开发时有效,部署后,都是程序名.exe.config。其实,也应该是改后者的。app.config主要保存默认配置,这可以手工改,要用户配置的,你现在的就可以