简体   繁体   English

是否有比使用StringCollection更好的方法来在应用程序配置文件中存储字符串列表?

[英]Is there a better way to store a list of strings in an applications config file than using a StringCollection?

I have a list of filenames I need to store in my applications config file. 我有一个文件名列表,需要存储在应用程序配置文件中。

The only collection that comes up in the initial combo box on the settings page is System.Collections.Specialized.StringCollection . 设置页面的初始组合框中出现的唯一集合是System.Collections.Specialized.StringCollection Now initially I didn't query this as I converted this to a List<string> straight away. 现在最初我没有查询它,因为我将其立即转换为List<string> There was one piece of code I wasn't happy with which was the copying of this list to and from the ListBox on the configuration dialog: 我不满意的一段代码是在配置对话框中的ListBox复制此列表:

public List<string> ImageNames
{
    get
    {
        return folderImages.Items.ToList();
    }
    set
    {
        folderImages.Items.AddRange(value.ToArray());
    }
}

However, I've been revisiting my code and thought that if I kept the list as a StringCollection I could improve this code. 但是,我一直在回顾自己的代码,并认为,如果将列表保留为StringCollection ,则可以改进此代码。 Everywhere else I used the list/collection was OK, but this conversion still isn't to my liking: 我在其他任何地方使用列表/集合都可以,但是这种转换仍然不符合我的喜好:

public StringCollection ImageNames
{
    get
    {
        var names = new StringCollection();
        names.AddRange(folderImages.Items.ToList().ToArray());
        return names;
    }
    set
    {
        value.ToList().ForEach(imageName => folderImages.Items.Add(imageName));
    }
}

Actually, now I see the code side-by-side (as it were) I'm thinking that the List<string> version is "cleaner" - it's certainly clearer. 实际上,现在我可以并排看到代码(就像以前一样),我认为List<string>版本更“干净”了-显然更清晰了。

So is there another collection or list type I can store in the settings file? 那么我可以在设置文件中存储其他集合或列表类型吗? I'd really like to avoid the conversion on reading and writing the settings file if at all possible. 如果可能的话,我真的很想避免在读取和写入设置文件时进行转换。

Failing that - is there a cleaner way of converting between a StringCollection and an ListBox.ObjectCollection (the ListBox.Items )? 失败-是否有更干净的方法在StringCollectionListBox.ObjectCollectionListBox.Items )之间转换?

您可以将System.Object存储到应用程序的设置中,而不是将其存储在应用程序的初始组合框中,但可以将其存储在应用程序的设置中,然后就可以直接在对象之间存储数据。

In the end I actually reverted to my original code, but with much cleaner conversions between the StringCollection and List<string> - the ugliness of which was my starting point. 最后,我实际上恢复了原始代码,但是在StringCollectionList<string>之间进行了更清晰的转换-丑陋是我的出发点。

So now on start up I have: 所以现在开始我有:

possibleImageNames = Properties.Settings.Default.Images.ToList();

and on shut down (where I update the config file) I have: 然后关闭(我在其中更新配置文件),我有:

Properties.Settings.Default.Images.Clear();
Properties.Settings.Default.Images.AddRange(possibleImageNames.ToArray());

Where the ToList method on the StringCollection is an extension method - used to encapsulate the looping code. 其中StringCollection上的ToList方法是扩展方法-用于封装循环代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM