NameValueCollection doesnt save to Properties.Sett

2019-07-12 18:55发布

I added an entry in the Properties.Settings named strCol that is of type NameValueCollection. I am using this to save items from a listview to the settings so that when the application reboots, the ListView gets re-populated again with the items that were saved previously. I'm only storing the item name and the tag from the ListViewItems.

But something strange is happening I cant explain.

I am using this code to save the items to the settings:

NameValueCollection nvCol = new NameValueCollection();
foreach (ListViewItem lviP in lvParmNames.Items)
{
    nvCol.Add(lviP.Text, lviP.Tag.ToString());
}
Properties.Settings.Default.strCol = nvCol;
Properties.Settings.Default.Save();

For some reason the Save and load in and from settings works fine, as long as the application is still active. But when the application has been closed and rebooted, the load doesn't work as if the NameValueCollection was not saved.

What am I missing here ?

How I'm currently loading the setting:

The call for loading, but it already seem to fail at the check if its null

if (Properties.Settings.Default.strCol != null)      
{        
    NameValueCollection nv = Properties.Settings.Default.strCol;
    foreach (string key in nv)
    {
        try
        {
            var value = nv[key];
            ListViewItem lvi = new ListViewItem(key);
            lvi.Tag = value;
            lvFamNames.Items.Add(lvi);
        }
        catch (Exception ex)
        {
            TaskDialog.Show("Error", "Error while parsing Family Names from saved settings. \n" + ex.Message);
        }
    }
}

Just noticed something:

settings

Though i added the NameValueCollection in the sections it is actually not added when building the application. When i open the config file, the setting is not there. So i guess the issue is here, though now i need to figure out how to add it now.

Looking into the Settings.Designer.cs file, i guess this part looks wrong for the type of 'NameValueCollection`:

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public global::System.Collections.Specialized.NameValueCollection strColExcelFamNames {
        get {
            return ((global::System.Collections.Specialized.NameValueCollection)(this["strCol"]));
        }
        set {
            this["strCol"] = value;
        }
    }

0条回答
登录 后发表回答