Search code examples
c#winformscheckedlistbox

checkedlistbox in C#, display only file name, not full path


I am trying to select a bunch of files and put the names into a checkedlistbox but it always displays the full directory path with the filename. I only want the user to see the file name but I want to preserve the path inside the code so when the user clicks a button the program can still find the files and operate on them.

My question has already been asked on another forum but I can't seem to get the expected results, currently, my code does as follows:

  1. user clicks button_1: User selects folder containing files

  2. all CSV files with only their file name in the checkedlistbox are displayed, with a message box appearing displaying their full paths. The user proceeds to check necessary files.

  3. User clicks button_2: Displays a message box with the checked filenames, but not the full file paths, of which I am trying to retrieve.

Any help in this would be most appreciated thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace SelectFiles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.CheckOnClick = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {

                checkedListBox1.Items.Clear();

                string[] files = Directory.GetFiles(fbd.SelectedPath);
                List<FileInfo> excel_files = new List<FileInfo>();

                foreach (string file in files)
                {
                    FileInfo f = new FileInfo(file);
                    MessageBox.Show((f.FullName));
                    excel_files.Add(f);
                }
                BindingSource bs = new BindingSource();
                bs.DataSource = excel_files;
                checkedListBox1.DataSource = bs;
                checkedListBox1.DisplayMember = "Name";//Path.GetFileName(file);
            }
        }
        private void button2_Click_1(object sender, EventArgs e)
        {

            List<FileInfo> list_all_excelfiles = new List<FileInfo>();
            foreach (FileInfo item in checkedListBox1.CheckedItems)
            {
                list_all_excelfiles.Add(item);
                MessageBox.Show(Path.GetFileName(item.FullName));
            }
        }
    }
}

Solution

  • If I understood correctly, you want to get the file full path when user click on button2.

    This can be achieved by modifying your code.

    In the button2 event, are asking for Path.GetFileName

    Change it to

    Path.GetFullPath
    

    which will return the full path of the file.

    Your code should be looks like :

    private void button2_Click_1(object sender, EventArgs e)
    {
      List<FileInfo> list_all_excelfiles = new List<FileInfo>();
      foreach (FileInfo item in checkedListBox1.CheckedItems)
        {
           list_all_excelfiles.Add(item);
           MessageBox.Show(Path.GetFullPath(item.Name));
        }
    }
    

    Note : in your code, you are trying to clear the items from checkedListBox1 by Clear() method but you'll face an exception.

    System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.'

    and that's because you added a data source already !

    instead use :

    checkedListBox1.DataSource = null;