如何存储在应用程序设置INT []数组 [英] How to store int[] array in application Settings

查看:127
本文介绍了如何存储在应用程序设置INT []数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个简单的Windows使用C#的前preSS 2008年我是一个有经验的C ++开发窗体应用程序,但我pretty很多全新的,以C#和.NET。

I'm creating a simple windows Forms application using C# express 2008. I'm an experienced C++ developer, but I am pretty much brand new to C# and .NET.

我目前存储一些使用的设置设计者和code这样我简单的应用程序设置:

I'm currently storing some of my simple application settings using the settings designer and code like this:

// Store setting  
Properties.Settings.Default.TargetLocation = txtLocation.Text;  
...  
// Restore setting  
txtLocation.Text = Properties.Settings.Default.TargetLocation;

现在我想将任int数组( INT [] ),或可能整数(列表℃的清单; INT > ),如设置。但是,我想不出如何做到这一点。我搜索的文档,计算器,和谷歌,而我无法找到如何做到这一点像样的解释。

Now I'd like to store either an array of ints ( int[] ), or possibly a List of ints ( List< int > ), as a setting. However, I can't figure out how to do this. I've searched the documentation, stackoverflow, and google, and I cannot find a decent explanation of how to do this.

我的基础上,稀疏的例子,我发现的直觉是,我要创建一个类是可序列化的包装我的数组或列表,然后我就可以使用该类型中的设置设计师。不过,我不完全知道如何做到这一点。

My hunch based on the sparse examples I've found is that I have to create a class that is serializable that wraps my array or List, and then I will be able to use that Type in the settings designer. However, I'm not sure exactly how to do this.

在此先感谢您的帮助!

推荐答案

还有另一种解决方案 - 需要一点点的设置文件的手工编辑的,但事后在VS环境,并在code正常工作。并且不需要额外的功能和包装。

There is also another solution - requires a bit of manual editing of the settings file, but afterwards works fine in VS environment and in the code. And requires no additional functions or wrappers.

问题是,那VS允许序列化 INT [] 默认设置文件类型 - 它只是不允许您在默认情况下选择它。
因此,创建所需的名称(例如SomeTestSetting)的设置,使任何类型(例如,字符串默认情况下)的。
保存更改。

The thing is, that VS allows to serialize int[] type by default in the settings file - it just doesn't allow you to select it by default. So, create a setting with desired name (e.g. SomeTestSetting) and make it of any type (e.g. string by default). Save the changes.

现在去你的项目文件夹并打开文本编辑器(记事本,例如)属性\\ Settings.settings文件,也可以在VS中右键单击在解决方案资源管理器中打开它的 - >属性 - > Settings.settings,选择打开方式,然后选择使用XML编辑器或来源$ C ​​$ C(文本)编辑器。
在打开的XML设置中找到您的设置(它看起来好像是这样):

Now go to your project folder and open the "Properties\Settings.settings" file with text editor (Notepad, for example) Or you can open it in VS by right-clicking in Solution Explorer on " -> Properties -> Settings.settings", select "Open With..." and then choose either "XML Editor" or "Source Code (Text) Editor". In the opened xml settings find your setting (it will look like this):

<Setting Name="SomeTestSetting" Type="System.String" Scope="User">
  <Value Profile="(Default)" />
</Setting>

从修改 System.String 类型参数为 System.Int32 [] 。现在,这部分将是这样的:

Change the "Type" param from System.String to System.Int32[]. Now this section will look like this:

<Setting Name="SomeTestSetting" Type="System.Int32[]" Scope="User">
  <Value Profile="(Default)" />
</Setting>

现在保存更改并重新打开项目设置 - 瞧! - 我们有类型的设置SomeTestSetting System.Int32 [] 可接,并通过设置VS设计器编辑(值也是如此),以及在code

Now save changes and re-open project settings - voilà! - We have the setting SomeTestSetting with type System.Int32[] which can be accessed and edited through VS Settings Designer (values too), as well as in the code.

这篇关于如何存储在应用程序设置INT []数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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