如何在应用程序设置中存储 int[] 数组 [英] How to store int[] array in application Settings
问题描述
我正在使用 C# express 2008 创建一个简单的 Windows 窗体应用程序.我是一名经验丰富的 C++ 开发人员,但我对 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.
我目前正在使用设置设计器和如下代码存储一些简单的应用程序设置:
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[]
),或者可能存储一个整数列表( List< int >
),作为一个设置.但是,我无法弄清楚如何做到这一点.我已经搜索了文档、stackoverflow 和 google,但找不到有关如何执行此操作的正确解释.
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 环境和代码中工作正常.并且不需要额外的函数或包装器.
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)并将其设为任何类型(例如默认情况下为 string
).保存更改.
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.
现在转到您的项目文件夹并使用文本编辑器(例如记事本)打开Properties\Settings.settings"文件,或者您可以通过在解决方案资源管理器"中右键单击-> 属性->"在 VS 中打开它Settings.settings",选择Open With...",然后选择XML Editor"或Source Code (Text) Editor".在打开的 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>
现在保存更改并重新打开项目设置 - 瞧!- 我们有类型为 System.Int32[]
的设置 SomeTestSetting,可以通过 VS 设置设计器(也有值)以及在代码中访问和编辑.
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屋!