简体   繁体   English

如何将新列表项添加到列表 <string> 在settings.settings中

[英]How to Add new listitem to List<string> in settings.settings

I am trying to add new list item to settings.settings file. 我正在尝试将新列表项添加到settings.settings文件。 I have modified my settings.settings file as below 我已经如下修改了settings.settings文件

在此处输入图片说明

and on button click i have the following code: 并在按钮上单击我有以下代码:

 private void button7_Click(object sender, EventArgs e)
        {
string tempConString = "server='.\sqlexpress';database='somedatabasename';Trusted_Connection=Yes;";
 Properties.Settings.Default.connectionString.Add(tempConString);
                                Properties.Settings.Default.Save();
}

i am getting NullReferenceException with message Object reference not set to an instance of an object. 我收到消息未将对象引用设置为对象实例的NullReferenceException。

I want to save multiple strings to Properties.Settings.Default.connectionString 我想将多个字符串保存到Properties.Settings.Default.connectionString

To achieve desired behavior you should follow the Accepted Answer of the post . 要实现期望的行为,您应该遵循帖子的“接受答案”。 I don't understand why a wrong answer is so upvoted. 我不明白为什么会提出错误的答案。

Never the less, just pick System.Collection.Specialized.StringCollection from Type DropDown and insert your values from new lines. 不过,只要从Type DropDown中选择System.Collection.Specialized.StringCollection并从新行中插入值即可。 And where you want to use it do something like this: 在您要使用它的地方执行以下操作:

Console.WriteLine(Settings.Default.test.Cast<string>().ToList().FirstOrDefault());

Everything was in that post, to be honest. 老实说,一切都在那个职位上。

Follow instructions from http://csharphelper.com/blog/2011/08/use-a-setting-that-contains-a-string-collection-in-c/ 请按照http://csharphelper.com/blog/2011/08/use-a-setting-that-c​​ontains-a-string-collection-in-c/中的说明进行操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM