【问题标题】:Problems while adding items to an array in the settings.settings在 settings.settings 中向数组添加项目时出现问题
【发布时间】:2016-08-01 02:26:28
【问题描述】:

我目前正在创建一个应用程序,用户可以在其中创建公告。我想保存用户公告并尝试了一切以设置形式保存用户输入。任何保存用户输入的建议/不同方法将不胜感激。

当前设置

我尝试添加新的“公告”:

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();
}

抛出错误

{"对象引用未设置为对象的实例。"}

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>

【问题讨论】:

    标签: c# .net arrays application-settings


    【解决方案1】:

    使用StringCollection 代替string[]。 VS 支持 StringCollection。

    【讨论】:

    • 更改代码后,我开始收到此错误 {"Object reference not set to an instance of an object."}
    • 您已更新您的问题,settings.settings xml file 现在应该可以使用了。但该异常听起来像是 VS 错误,所以请尝试重新启动它。
    猜你喜欢
    • 2020-11-18
    • 2014-01-14
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    相关资源
    最近更新 更多