I would highly suggest storing information in a json file rather than under settings.
Using NewtonSoft NuGet package
public static class JsonHelpers
{
public static string FileName = "Items.json";
/// <summary>
/// Create a mockup if file does not exists
/// </summary>
public static void InitializeData()
{
if (!File.Exists(FileName))
{
File.WriteAllText(JsonHelpers.FileName, JsonConvert.SerializeObject(MockOperations.List, Formatting.Indented));
}
}
/// <summary>
/// Read items
/// </summary>
/// <returns></returns>
public static List<FileItem> Read() =>
JsonConvert.DeserializeObject<List<FileItem>>(File.ReadAllText(JsonHelpers.FileName));
/// <summary>
/// Save items
/// </summary>
/// <param name="list"></param>
public static void Save(List<FileItem> list) =>
File.WriteAllText(FileName, JsonConvert.SerializeObject(list, Formatting.Indented));
}
Container class
public class FileItem : INotifyPropertyChanged
{
private string _fullName;
public string FullName
{
get => _fullName;
set
{
_fullName = value;
OnPropertyChanged();
}
}
[JsonIgnore]
public string FileName => Path.GetFileName(FullName);
public override string ToString() => FileName;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Form code
public partial class Form1 : Form
{
public BindingSource _BindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
JsonHelpers.InitializeData();
_BindingSource.DataSource = JsonHelpers.Read();
listBox1.DataSource = _BindingSource;
listBox1.MouseDoubleClick += ListBox1OnMouseDoubleClick;
Closing += OnClosing;
}
private void OnClosing(object sender, CancelEventArgs e)
{
JsonHelpers.Save((List<FileItem>)_BindingSource.DataSource);
}
private void ListBox1OnMouseDoubleClick(object sender, MouseEventArgs e)
{
if (_BindingSource.Current == null) return;
if (!label1.Visible)
{
label1.Visible = true;
}
label1.Text = ((FileItem) _BindingSource.Current).FullName;
}
private void AddNewButton_Click(object sender, EventArgs e)
{
_BindingSource.Add(new FileItem() {FullName = "D:\\Docs\\readme.md"});
}
}