Click here to Skip to main content
15,889,200 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Day, I Have A List<string> Saved In My App User Settings, this List is supposed to save the file paths of a openfiledialog to then deserialize it, but I already Called the method Save(); And even the method Upgrade(); After adding the strings to the list, but this strings doesn´t save. Why?

What I have tried:

To Save It:
private void ImportIcon_Click(object sender, EventArgs e)
{
IfSound();
openFileDialog1.Filter = "Analytica Files (*.analy) |*.analy";
openFileDialog1.Multiselect = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
var Path = openFileDialog1.FileName;
foreach (string file in openFileDialog1.FileNames)
{
Settings.Default.FileList.Add(file);
Settings.Default.Save();
//Settings.Default.Upgrade();
}
Posted
Updated 9-Jul-18 15:01pm

1 solution

Check the Setting Type. Application Settings cannot be updated without a custom handler - refer; Application Settings Overview | Microsoft Docs[^]
Use your debugger, if the setting is defined but not initialised, it will be null & you cannot add values to it - the blow code works in a Test application - the values are carried over restarts of the application - create a User Setting named TestList to test;
C#
if(Properties.Settings.Default.TestList == null)
    {
        MessageBox.Show("Not initialised");
    // Create a new List and Save to Settings        
    Properties.Settings.Default.TestList = new System.Collections.Specialized.StringCollection();
        Properties.Settings.Default.Save();
    }
    else if(Properties.Settings.Default.TestList.Count > 0)
    {
        // Create a message showing the last value in the list and the count of items
        string strMsg = string.Format("List is populated\r\nLast Value: {0}\r\nCount: {1}", Properties.Settings.Default.TestList[Properties.Settings.Default.TestList.Count - 1].ToString(), Properties.Settings.Default.TestList.Count.ToString());
           
    MessageBox.Show(strMsg);
    }
    else
    {
        MessageBox.Show("Currently empty");
    }

    string[] myTest = new string[] { "Value 1", "Value 2" };
    
    foreach(string myVal in myTest)
    {
        Properties.Settings.Default.TestList.Add(myVal);
    }
    Properties.Settings.Default.Save();

    if (Properties.Settings.Default.TestList.Count > 0)
    {
        MessageBox.Show("After populate\r\n" + Properties.Settings.Default.TestList[0].ToString()
            + "\r\nCount: " + Properties.Settings.Default.TestList.Count);
    }


Kind Regards
 
Share this answer
 
Comments
SebGM2018 10-Jul-18 0:09am    
And What Is This For?, What Is The Exact Solution?
an0ther1 10-Jul-18 17:40pm    
Sorry but I cannot tell you what the exact solution is because I cannot see your screen or access your computer.
The code you provided should save to a Setting named FileList unless it is an Application setting or the Setting is never initialised - read the documentation in the provided link & debug your code to determine which problem you have

Kind Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900