如何在应用程序设置中存储队列 [英] how to store a Queue in application settings
问题描述
嘿,
我希望存储队列< guid>在我的应用程序设置中这样做的原因是存储最后加载的10个对象的列表,并在我的应用程序上提供启动屏幕。
我有MyApp.Properties.Settings。 Default.Queue设置但我不知道如何将其类型定义为guid队列。
这可能吗?或者我是否需要首先将其翻译成其他内容,如果有的话会被推荐什么?
谢谢
Hey,
I wish to store a queue<guid> in my application settings. The reason for this is to store a list of the last 10 or so objects loaded and provide a startup screen on my application.
I have MyApp.Properties.Settings.Default.Queue set up but im not sure how to define its type as a queue of guid.
is this possible? or do i need to translate it into something else first, if so what would be reccomended?
Thanks
推荐答案
我在beginnig上没有仔细阅读,现在我更新了我的解决方案Queue< Guid>
。
你可以转换Guid它的字符串表示和保存队列作为字符串集合。您可以将
I didn't read too carefully at the beginnig, now I updated my solution forQueue<Guid>
.
You can convert Guid to it's string representation and save queue as collection of strings. You can declare your
MyApp.Properties.Settings.Default.Queue
声明为
System.Collections.Specialized.StringCollection
然后使用表格的OnLoad和OnClosed事件来读/写您的设置:
Then use form's OnLoad and OnClosed events to read/write your settings:
private Queue<Guid> _queue = new Queue<Guid>();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
foreach (string item in Properties.Settings.Default.Queue)
{
var guid = Guid.Parse(item);
_queue.Enqueue(guid);
}
}
protected override void OnClosed(EventArgs e)
{
Properties.Settings.Default.Queue.Clear();
while(_queue.Count > 0)
{
Guid guid = _queue.Dequeue();
Properties.Settings.Default.Queue.Add(guid.ToString());
}
Properties.Settings.Default.Save();
base.OnClosed(e);
}
您可以随时使用自定义类型,也可以编写自定义配置部分。
我希望你会发现这很有用:)
You can always use your custom type or you can write your custom configuration section.
I hope you'll find this useful :)
这篇关于如何在应用程序设置中存储队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!