简体   繁体   English

初始化 StringCollection 设置

[英]Initializing a StringCollection Setting

I am writing an application, and I want to store a list of files selected by the user.我正在编写一个应用程序,我想存储用户选择的文件列表。 Currently, one of my settings is a StringCollection called filesToFetch, which is User scoped and contains the paths of all the files that the program should fetch.目前,我的设置之一是名为 filesToFetch 的 StringCollection,它是用户范围的,包含程序应获取的所有文件的路径。 I have a button that allows the user to add new files to the list.我有一个按钮,允许用户将新文件添加到列表中。 This is the code for the button click event这是按钮点击事件的代码

private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = openFileDialog1.ShowDialog();
    if (result == DialogResult.OK)
    {
        Properties.Settings.Default.filesToFetch.Add(openFileDialog1.FileName);
        Properties.Settings.Default.Save();
    }
}

When I try to add a new file to the StringCollection, I get the error当我尝试向 StringCollection 添加新文件时,出现错误

NullReference Exception was unhandled NullReference 异常未处理

Object reference not set to an instance of an object.你调用的对象是空的。

I think that this may be happening because filesToFetch has not been initialized, but I'm not really sure.我认为这可能是因为 filesToFetch 尚未初始化,但我不确定。 I could be wrong, but I thought that an object gets a name when it is initialized, and since my settings all get names at Design time, I assumed that they are automatically initialized when the program runs, but now I think I might be wrong about this.我可能是错的,但我认为对象在初始化时会得到一个名称,并且由于我的设置都是在设计时获取名称,因此我认为它们在程序运行时会自动初始化,但现在我认为我可能错了对这个。 Is this the issue, or am i missing something else?这是问题,还是我错过了其他东西?

Here is a screen capture of my settings for reference.这是我的设置的屏幕截图以供参考。

属性设置

If you want to enter values in the Settings GUI, on the far right there is a "..." button which allows you to enter the initial string values each separated on a line.如果您想在设置 GUI 中输入值,在最右侧有一个“...”按钮,它允许您输入初始字符串值,每个值都在一行中分开。 It then converts that into XML as such:然后将其转换为 XML:

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <string>String1</string>
  <string>String2</string>
</ArrayOfString>

edit: Yes you need to initialize the StringCollection and my above answer is the way to do it using the GUI.编辑:是的,您需要初始化 StringCollection,我上面的答案是使用 GUI 进行操作的方法。 Thought it would help people (like me) who stumbled on this post looking for a way to initialize a StringCollection setting like OP needed to do.认为它会帮助那些偶然发现这篇文章的人(像我一样)寻找一种方法来初始化像 OP 需要做的 StringCollection 设置。

I should probably explain a bit further.我可能应该进一步解释一下。 Let's say you were going to use a list of strings.假设您要使用字符串列表。 You can declare:您可以声明:

IList<string> a;

At this point a = null and null does not have an Add method.此时 a = null 和 null 没有 Add 方法。 If you initialize:如果初始化:

IList<string> a = new List<string>();

Now a = an empty list of strings.现在 a = 一个空的字符串列表。 It will at this point have an Add method to use to add strings to the list.此时它将有一个 Add 方法用于将字符串添加到列表中。

I had a similar issue using the add method, but insert with index and value parameters worked fine.我在使用 add 方法时遇到了类似的问题,但使用索引和值参数插入工作正常。

https://msdn.microsoft.com/en-us/library/system.collections.specialized.stringcollection.insert%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/library/system.collections.specialized.stringcollection.insert%28v=vs.110%29.aspx

Something like this might work:像这样的事情可能会奏效:

private void button1_Click(object sender, EventArgs e)
{
    DialogResult result = openFileDialog1.ShowDialog();
    if (openFileDialog1.ShowDialog(this) != DialogResult.OK)
        return;
    int x = 0;
    foreach (String file in openFileDialog1.FileNames)
    {
        Properties.Settings.Default.activeFiles.Insert(x, openFileDialog1.Filename);
        x++;
    }

    Properties.Settings.Default.Save();
}

Though the problem is pretty tricky but the solution is simple.虽然问题很棘手,但解决方案很简单。 Create a settings as you did.像您一样创建设置。 Copy & Paste the following code into the place of Default Value & continue working in your way.将以下代码复制并粘贴到Default Value的位置并继续按照您的方式工作。

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

Explanation Use a string collection setting in C#说明在 C# 中使用字符串集合设置

Hope this helps.希望这可以帮助。 Thank you.谢谢你。

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

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