Reputation: 3177
In my Settings.settings file I have 10 bool items, I am trying to save them all to the list, then iterate through them, change their values and then save it back to the settings file but it doesn't work.
Here is short example:
public List<bool> list = new List<bool>();
list.Add(Properties.Settings.Default.Item0);
list.Add(Properties.Settings.Default.Item1);
list.Add(Properties.Settings.Default.Item2);
list.Add(Properties.Settings.Default.Item3);
list.Add(Properties.Settings.Default.Item4);
// ... up to 10
Then I am trying to iterator through them:
for (int i = 0; i < list.Count; i++)
{
if (listBox1.GetSelected(i))
{
list[i] = true;
}
else
{
list[i] = false;
}
}
Properties.Settings.Default.Save(); // it sets list items properly but it doesn't actually save it to the user settings file
so if I'll do something like this:
if (listBox1.GetSelected(0))
{
Properties.Settings.Default.Item0 = true;
}
else
{
Properties.Settings.Default.Item0 = false;
}
Properties.Settings.Default.Save();
it will save it and next time I am opening my app I'll get the correct saved values, but I don't want to have inefficiency with 10 if statements for each item
Upvotes: 2
Views: 2328
Reputation: 1608
You could access the settings by name, like so.
Settings.Default["Item1"];
Since you have the naming convention Item + Incrementing Number in your for loop you could do this,
for (int i = 0; i < list.Count; i++)
{
Settings.Default["Item" + i.ToString()] = listBox1.GetSelected(i);
}
Properties.Settings.Default.Save();
Upvotes: 2