向 settings.settings 中的数组添加项目时出现问题
Problems while adding items to an array in the settings.settings
我目前正在创建一个用户可以创建公告的应用程序。我想保存用户公告,并已尝试一切以设置的形式保存用户输入。任何用于保存用户输入的 suggestions/different 方法将不胜感激。
当前设置
我尝试添加新的"Announcements":
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Title[Properties.Settings.Default.MessageNum] = textBox1.Text;
Properties.Settings.Default.Message[Properties.Settings.Default.MessageNum] = textBox2.Text;
Properties.Settings.Default.MessageNum++;
Properties.Settings.Default.Save();
}
抛出错误
{"Object reference not set to an instance of an object."}
settings.settings xml 请求的文件
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WindowsFormsApplication1.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="Title" Type="System.String[]" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="Message" Type="System.String[]" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="MessageNum" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
</Settings>
</SettingsFile>
使用 StringCollection
而不是 string[]
。 VS支持StringCollection
我目前正在创建一个用户可以创建公告的应用程序。我想保存用户公告,并已尝试一切以设置的形式保存用户输入。任何用于保存用户输入的 suggestions/different 方法将不胜感激。
当前设置
我尝试添加新的"Announcements":
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Title[Properties.Settings.Default.MessageNum] = textBox1.Text;
Properties.Settings.Default.Message[Properties.Settings.Default.MessageNum] = textBox2.Text;
Properties.Settings.Default.MessageNum++;
Properties.Settings.Default.Save();
}
抛出错误
{"Object reference not set to an instance of an object."}
settings.settings xml 请求的文件
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="WindowsFormsApplication1.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="Title" Type="System.String[]" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="Message" Type="System.String[]" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="MessageNum" Type="System.Int32" Scope="User">
<Value Profile="(Default)">0</Value>
</Setting>
</Settings>
</SettingsFile>
使用 StringCollection
而不是 string[]
。 VS支持StringCollection