Hello,
A suggestion shown below, full source in this repository.
- Note there is room for minor improvement
- You need to alter the selection of the file from a static string to using an open dialog
Create a class
public class MediaItem
{
public string FileName { get; set; }
public override string ToString() => File.Exists(FileName) ? Path.GetFileName(FileName) : "";
}
Create code to save/load from a file
/// <summary>
/// Load file names into a BindingSource which in turn becomes the DataSource for a ListBox
/// </summary>
/// <param name="sender"></param>
/// <returns></returns>
public static bool LoadFromFile(this BindingSource sender)
{
if (File.Exists(FileOperations.MediaFileName))
{
var lines = File.ReadAllLines(FileOperations.MediaFileName).Where(x => !string.IsNullOrWhiteSpace(x));
sender.DataSource = (lines.Where(File.Exists).Select(line => new MediaItem() {FileName = line})).ToList();
return true;
}
else
{
return false;
}
}
/// <summary>
/// Save contents of ListBox to a file
/// </summary>
/// <param name="sender"></param>
/// <param name="FileName"></param>
public static void SaveToFile(this BindingSource sender, string FileName)
{
File.WriteAllLines(FileName, (from Row in sender.Cast<MediaItem>() select Row.FileName).ToArray());
}
}
A static class for file names, in a real app you would get the file name as currently doing. There are two properties, one for load one for save so you can test functionality then for real use use MediaFileName
public class FileOperations
{
/// <summary>
/// For a real app this would no be hard coded
/// </summary>
public static string MediaFileName => "Mediasongs.txt";
/// <summary>
/// For a real app we save to <see cref="MediaFileName"/> but this helps
/// to ensure code is working correctly
/// </summary>
public static string SaveFileName => "MediasongsTest.txt";
}
Code to wrap MessageBox
public static class Dialogs
{
public static bool Question(string text, string title = "Question") =>
(MessageBox.Show(text, title, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes);
}
Form code
using System;
using System.Windows.Forms;
using MediaFileDemo.Classes;
using static MediaFileDemo.Classes.Dialogs;
namespace MediaFileDemo
{
public partial class Form1 : Form
{
private readonly BindingSource _bindingSource = new BindingSource();
public Form1()
{
InitializeComponent();
Shown += OnShown;
}
private void OnShown(object sender, EventArgs e)
{
_bindingSource.LoadFromFile();
listBox1.DataSource = _bindingSource;
}
private void GetFileNameButton_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
MessageBox.Show(((MediaItem)listBox1.SelectedItem).FileName);
}
}
private void SaveButton_Click(object sender, EventArgs e)
{
_bindingSource.SaveToFile("Test.txt");
}
private void DeleteCurrentButton_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
if (Question("Delete"))
{
_bindingSource.Remove((MediaItem)listBox1.SelectedItem);
}
}
}
}
}
I don't have any media files so I just used random files.