您必须首先在项目设置文件中创建一个设置,我们将其命名为CustomClasses
. 下一部分有点棘手,因为它涉及编辑Settings.settings文件的 XML:
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings"
CurrentProfile="(Default)"
GeneratedClassNamespace="ConsoleApplication1.Properties"
GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="CustomClasses"
GenerateDefaultValueInCode="false"
Type="System.Collections.Generic.List<ConsoleApplication1.CustomClass>"
Scope="User">
</Setting>
</Settings>
</SettingsFile>
如果您打开Settings.Designer.cs文件,您现在应该有:
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Collections.Generic
.List<ConsoleApplication1.CustomClass> CustomClasses {
get {
return ((global::System.Collections.Generic
.List<ConsoleApplication1.CustomClass>)(this["CustomClasses"]));
}
set {
this["CustomClasses"] = value;
}
}
您可以将设置保存在您的应用程序中:
class Program
{
static void Main(string[] args)
{
Properties.Settings.Default.CustomClasses = new List<CustomClass>() {
new CustomClass(){Columns="columns1"},
new CustomClass(){Columns="columns2"},
new CustomClass(){Columns="columns3"},
new CustomClass(){Columns="columns4"}
};
Properties.Settings.Default.Save();
}
}