Resolved I want to save full path of listbox in C#

sddd1

Member
Joined
Jul 22, 2021
Messages
14
Programming Experience
1-3
First Of All I add Directory in Listbox, Like A Directory, B Directory. When User Click the item of ListBox ,Get the full path of Items in C#
Someone modify the code !
Thanks in Advance !
C#:
BindingSource listboxsource = null;
public Form1()
{
    InitializeComponent();
    listboxsource = new BindingSource(Properties.Settings.Default.listboxitems, "");
    someListbox.DataSource = listboxsource;

}

//Add Folder in Listbox
private void button57_Click(object sender, EventArgs e)
{

    FolderBrowserDialog dialog = new FolderBrowserDialog();

    dialog.Description = "Please Select Folder";

    if (dialog.ShowDialog() == DialogResult.OK)
    {

        string folderName = dialog.SelectedPath;

        string s = Path.GetFileName(folderName);
        listboxsource.Add(s);

        Properties.Settings.Default.Save();
        listboxsource = new BindingSource(Properties.Settings.Default.listboxitems, "");
        someListbox.DataSource = listboxsource;   
    }
}

        private void listBox5_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string fullname = Properties.Settings.Default.FullName;


                Properties.Settings.Default.Save();
                MessageBox.Show(fullname);
            }
            catch(Exception)
            { }
        }
 
Last edited by a moderator:
Solution
It looks like you are using application settings to store the data but you might be thinking about using separate lists for the names and the full paths. What you ought to do is just use a single list containing the full paths. In the UI, you can create DirectoryInfo objects using those full paths and then bind a list of them to your control and just display the Name property, which is the folder name without the path. These are the steps I would recommend:

1. Open the Settings page of the project properties. Add an appropriately-named setting of type System.Collections.Specialized.StringCollection with User scope.
2. From the Value field, open the editor, add an item and click OK. Open the...
It looks like you are using application settings to store the data but you might be thinking about using separate lists for the names and the full paths. What you ought to do is just use a single list containing the full paths. In the UI, you can create DirectoryInfo objects using those full paths and then bind a list of them to your control and just display the Name property, which is the folder name without the path. These are the steps I would recommend:

1. Open the Settings page of the project properties. Add an appropriately-named setting of type System.Collections.Specialized.StringCollection with User scope.
2. From the Value field, open the editor, add an item and click OK. Open the editor again and remove the item and click OK. This is to generate the XML code that creates the StringCollection object so you don't have to do it in code.
3. Add a BindingSource to the form in the designer.
4. In the Load event handler of the form, get the contexts of your settings collection, create DirectoryInfo objects and bind a list of them to your ListBox:
C#:
private void Form1_Load(object sender, EventArgs e)
{
    var folders = Properties.Settings
                            .Default
                            .FolderPaths
                            .Cast<string>()
                            .Select(s => new DirectoryInfo(s))
                            .ToList();

    foldersBindingSource.DataSource = folders;
    foldersListBox.DisplayMember = nameof(DirectoryInfo.Name);
    foldersListBox.ValueMember = nameof(DirectoryInfo.FullName);
    foldersListBox.DataSource = foldersBindingSource;
}
5. When you want to add a new item, create a new DirectoryInfo object and add it to the BindingSource:
C#:
private void button1_Click(object sender, EventArgs e)
{
    using (var dialogue = new FolderBrowserDialog())
    {
        if (dialogue.ShowDialog() == DialogResult.OK)
        {
            foldersBindingSource.Add(new DirectoryInfo(dialogue.SelectedPath));
        }
    }
}
6. On exit, copy the folder paths to the settings collection and save:
C#:
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
    Properties.Settings.Default.FolderPaths.Clear();
    Properties.Settings
              .Default
              .FolderPaths
              .AddRange(foldersBindingSource.Cast<DirectoryInfo>()
                                            .Select(di => di.FullName)
                                            .ToArray());
    Properties.Settings.Default.Save();
}
 
Solution

Latest posts

Back
Top Bottom