将ArrayList保存在User.Config中 [英] Saving ArrayList in User.Config

查看:48
本文介绍了将ArrayList保存在User.Config中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我花了一些时间将充满对象的ArrayList保存到user.config文件中.

简而言之,我的程序确实以下内容:

1)加载并检查ArrayList的user.config-如果列表为空,则应用程序将创建带有少量对象的基本列表
2)然后,应用程序调用Properties.Settings.Default.Save();保存新创建的ArrayList

此时,我在user.config文件中看到以下内容,其中的特定设置应为:

Hi all,

I am having a bit of a rough time saving an ArrayList full of objects to the user.config file.

In short, my program does the following:

1) Loads and checks user.config for the ArrayList - if the list is null, the application creates a basic list with a handful of objects
2) The application then calls Properties.Settings.Default.Save(); to save the newly created ArrayList

At this point, I see the following in my user.config file where the particular setting should be:

<setting name="presetList" serializeAs="Xml">
    <value />
</setting>



如果我选择输入配置窗格",在我正在编写的程序中,新表格试图读取"presetList".从user.config文件中.由于我能够访问在应用程序主要部分中创建的对象的ArrayList,因此似乎成功完成了此操作.但是,随后再尝试将此列表保存回user.config文件,都将导致上面显示的空XML.

是否对为什么user.config文件中什么都没有显示出任何想法?我已检查以确保查看的是正确的文件.

关于为什么该应用程序似乎没有问题,以另一种形式读回ArrayList的任何想法,但是拒绝将数据写入到该文件中. user.config文件?

任何帮助将不胜感激……我整天都在这上面撞墙.



If I opt to enter the "Configuration Pane" of the program I am writing, the new form tries to read the "presetList" from the user.config file.  It seems to do so successfully as I am able to access the ArrayList of objects created in the main portion of the application.  However, any subsequent attempts to save this list back to the user.config file results in the same empty XML shown above.

Any thoughts as to why nothing is showing up in the user.config file?  I have checked to ensure that I am looking at the correct file.

Any thoughts as to why the application seems to have no issues reading the ArrayList back in on another form, but refuses to write the data to the user.config file?

Any help would be greatly appreciated...I have been banging my head against the wall all day on this one.

推荐答案

 static void AddUpdateAppSettings(string key, string value)
        {
            try
            {
                var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                var settings = configFile.AppSettings.Settings;
                if (settings[key] == null)
                {
                    settings.Add(key, value);
                }
                else
                {
                    settings[key].Value = value;
                }
                configFile.Save(ConfigurationSaveMode.Modified);
                ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
            }
            catch (ConfigurationErrorsException)
            {
                Console.WriteLine("Error writing app settings");
            }
        }


这篇关于将ArrayList保存在User.Config中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆