public void saveValue(string Name, string Value)
        {
            //ConfigurationManager.RefreshSection(Name);
            ConfigurationManager.AppSettings.Set(Name, Value);            Configuration config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath);
            config.AppSettings.Settings[Name].Value = Value;
            config.Save(ConfigurationSaveMode.Modified);
            //config.fl
            config = null;
        }
用上面的函数总是等到程序运行结束,才将数据保存进去,哪位知道怎么立即保存?

解决方案 »

  1.   

    我给你一个思路:你可以建立一个专门读写XML的类来操作Web.config,通过这个类来读写XML文件,达到动态修改的效果!
      

  2.   

    无法立即保存,这是app.config的机制,在程序运行结束后才会更新至app.config如果你想保存后在程序中就运用,那就保存在一个公共类或是类的属性中.再加个更新标志在每次调用的时候进行判断,如果更新过就调用属性里的,如果没有更新就取app.config里的
      

  3.   

    之前为了保存数据库连接字符串,我有被app.config搞晕头了.
      

  4.   

            private static string _appconfig = null;        /// <summary>
            /// 获取或设置需要配置或读取的App.Config的文件名,若赋值为空(或未曾赋值)则下次读取该属性将取得调用方的EXE的默认config,否则返回上次设置的值。
            /// </summary>
            public static string AppConfig
            {
                get
                {
                    if (_appconfig == null)
                    {
                        Type t = typeof(System.Configuration.ConfigurationManager).Assembly.GetType("System.Configuration.ClientConfigurationHost");
                        object cfghst = Activator.CreateInstance(t, true);
                        PropertyInfo pi = t.GetProperty("ConfigPaths", BindingFlags.Instance | BindingFlags.NonPublic);
                        object cfgpath = pi.GetValue(cfghst, null);                    Type t1 = typeof(System.Configuration.ConfigurationManager).Assembly.GetType("System.Configuration.ClientConfigPaths");
                        pi = t1.GetProperty("ApplicationConfigUri", BindingFlags.Instance | BindingFlags.NonPublic);
                        string path = (string)pi.GetValue(cfgpath, null);                    if (string.IsNullOrEmpty(path))
                            _appconfig = string.Empty;
                        else
                            _appconfig = path.Replace(".vshost.", ".");
                    }
                    return _appconfig;
                }
                set
                {
                    _appconfig = value;
                }
            }
            /// <summary>
            /// 提供直接修改AppConfig内配置值得方法
            /// </summary>
            /// <param name="key">键</param>
            /// <param name="value">值</param>
            public static void SetSettingToAppConfig(string key, string value)
            {
                if (string.IsNullOrEmpty(key))
                {
                    throw new Exception("AppConfig的配置中,key不能为空");
                }
                else
                {
                    key = key.Trim();
                }
                if (string.IsNullOrEmpty(value))
                    value = "";
                else
                    value = value.Trim();            if (!File.Exists(AppConfig))
                {
                    throw new DirectoryNotFoundException();
                }
                File.SetAttributes(AppConfig, FileAttributes.Normal);
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(AppConfig);
                XmlNodeList xmllst = xmldoc.SelectNodes("/configuration/appSettings/add");
                if (xmldoc.SelectSingleNode("/configuration/appSettings") == null)
                {
                    XmlNode n2 = xmldoc.CreateNode("element", "appSettings", "");
                    n2.InnerXml = "<add key=\"" + key + "\" value=\"" + value + "\"/>";
                    xmldoc.SelectSingleNode("/configuration").AppendChild(n2);
                    xmldoc.Save(AppConfig);
                }
                else if (xmllst.Count == 0)
                {
                    XmlNode n2 = xmldoc.CreateNode("element", "add", "");
                    XmlAttribute xa = xmldoc.CreateAttribute("key");
                    xa.Value = key;
                    n2.Attributes.Append(xa);
                    xa = xmldoc.CreateAttribute("value");
                    xa.Value = value;
                    n2.Attributes.Append(xa);
                    xmldoc.SelectSingleNode("/configuration/appSettings").AppendChild(n2);
                    xmldoc.Save(AppConfig);
                }
                else
                {
                    bool existed = false;
                    foreach (XmlNode n1 in xmllst)
                    {
                        if (n1.Attributes["key"].Value.ToUpper() == key.ToUpper())
                        {
                            n1.Attributes["value"].Value = value;
                            xmldoc.Save(AppConfig);
                            existed = true;
                            break;
                        }
                    }
                    if (!existed)
                    {
                        XmlNode xmlnd = xmldoc.SelectSingleNode("/configuration/appSettings");
                        XmlNode n2 = xmldoc.CreateNode("element", "add", "");
                        XmlAttribute xa = xmldoc.CreateAttribute("key");
                        xa.Value = key;
                        n2.Attributes.Append(xa);
                        xa = xmldoc.CreateAttribute("value");
                        xa.Value = value;
                        n2.Attributes.Append(xa);
                        xmlnd.AppendChild(n2);
                        xmldoc.Save(AppConfig);
                    }
                }
                ConfigurationManager.RefreshSection("appSettings");
            }
      

  5.   

    这个我试过,不通。好像.net里的缓存机质在作怪,在释放对象的时候才保存!
      

  6.   

    搞不懂 为什么appconfig不能随时保存啊? 要想做到保存,最后还是要用到xmlDocument来操作。  我现在只看到他拿值的时候方便,更新是个很头痛的问题啊。。   appConfig ----徘徊ing----Xml