将副本应用于您的案例(我无法标记它,因为我无法撤销关闭,我发布此答案是为了解决您的困难以及比各种教程和副本更精确和更完整):
How to save a List<string> on Settings.Default?
在例如下面的命名空间中具有可序列化结构:
namespace WindowsFormsAppTest
{
[Serializable]
public struct Point
{
public uint xPoint { get; set; }
public uint yPoint { get; set; }
public Point(uint x, uint y)
{
xPoint = x;
yPoint = y;
}
}
}
如前所述,属性必须是可读写的,所以我添加了自动设置器,并且结构必须通过添加属性来序列化。
编译项目。
您需要创建一个字符串参数,例如名称为MyList:
然后使用任何文本编辑器手动编辑Settings.settings,将其类型更改为:
System.Collections.Generic<WindowsFormsAppTest.Point>
在 HTML 文本编码中,如:
System.Collections.Generic.List<WindowsFormsAppTest.Point>
设置.设置
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WindowsFormsAppTest.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="MyList" Type="System.Collections.Generic.List<WindowsFormsAppTest.Point>" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
</SettingsFile>
保存后,去Visual Studio重新加载文件,就可以看到类型改变了:
您需要使用设计器更新此设置生成的 C# 代码文件,方法是扩展参数类型并单击此自定义列表类型:
如果没有,或者出现问题,您需要手动更新这些文件:
app.config 到 serializeAs Xml
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WindowsFormsAppTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/></startup>
<userSettings>
<WindowsFormsAppTest.Properties.Settings>
<setting name="MyList" serializeAs="Xml">
<value />
</setting>
</WindowsFormsAppTest.Properties.Settings>
</userSettings>
</configuration>
Settings.Designer.cs 更改属性的类型
public global::System.Collections.Generic.List<WindowsFormsAppTest.Point> MyList {
get {
return ((global::System.Collections.Generic.List<WindowsFormsAppTest.Point>)(this["MyList"]));
}
全部保存和/或重新编译。
现在,您可以在 Main 方法或主窗体的构造函数或加载事件处理程序中编写示例:
private void FormTest_Load(object sender, EventArgs e)
{
if ( Properties.Settings.Default.MyList == null )
Properties.Settings.Default.MyList = new List<Point>();
Properties.Settings.Default.Save();
}
例如在按钮单击事件处理程序中:
private void ButtonCreate_Click(object sender, EventArgs e)
{
Properties.Settings.Default.MyList.Add(new Point(10, 10));
Properties.Settings.Default.MyList.Add(new Point(10, 20));
Properties.Settings.Default.MyList.Add(new Point(20, 20));
Properties.Settings.Default.MyList.Add(new Point(50, 50));
Properties.Settings.Default.Save();
}
现在配置文件是:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<userSettings>
<WindowsFormsAppTest.Properties.Settings>
<setting name="MyList" serializeAs="Xml">
<value>
<ArrayOfPoint xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Point>
<xPoint>10</xPoint>
<yPoint>10</yPoint>
</Point>
<Point>
<xPoint>10</xPoint>
<yPoint>20</yPoint>
</Point>
<Point>
<xPoint>20</xPoint>
<yPoint>20</yPoint>
</Point>
<Point>
<xPoint>50</xPoint>
<yPoint>50</yPoint>
</Point>
</ArrayOfPoint>
</value>
</setting>
</WindowsFormsAppTest.Properties.Settings>
</userSettings>
</configuration>
我们可以写来改变一个项目,因为是一个结构所以一个值类型:
private void ButtonUpdate_Click(object sender, EventArgs e)
{
var point = Properties.Settings.Default.MyList[0];
point.xPoint = 100;
point.yPoint = 100;
Properties.Settings.Default.MyList[0] = point;
Properties.Settings.Default.Save();
}
现在设置文件有:
<Point>
<xPoint>100</xPoint>
<yPoint>100</yPoint>
</Point>
...
为了测试这一点,我们可以使用另一个按钮列出项目:
private void ButtonShow_Click(object sender, EventArgs e)
{
var list = Properties.Settings.Default.MyList.Select(p => $"{p.xPoint}, {p.yPoint}");
MessageBox.Show(string.Join(Environment.NewLine, list.ToArray()));
}
注意
要清理应用程序设置,您需要删除所有这些文件夹:
c:\Users\User\AppData\Local\Organization\WindowsFormsAppTest.exe_Url_*
其中Organization 和WindowsFormsAppTest 来自AssemblyInfo.cs 中使用字段AssemblyCompany 和AssemblyTitle 定义的清单文件。