C#中的checkedlistbox,只显示文件名,不显示完整路径

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.

我的问题已经在另一个论坛上问过了,但我似乎无法得到预期的结果,目前,我的代码如下:

  1. 用户点击button_1:用户选择包含文件的文件夹

  2. 显示选中列表框中只有文件名的所有 CSV 文件,并出现一个显示其完整路径的消息框。用户继续检查必要的文件。

  3. 用户点击 button_2:显示一个消息框,其中包含我正在尝试检索的已检查文件名,但不是完整文件路径。

如有任何帮助,我们将不胜感激。

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));
            }
        }
    }
}

如果我没理解错的话,你想在用户点击 button2 时获得文件完整路径

这可以通过修改您的代码来实现。

在button2事件中,正在请求Path.GetFileName

改为

Path.GetFullPath

which will return the full path of the 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.GetFullPath(item.Name));
    }
}

注意 :在您的代码中,您试图通过 Clear() 方法从 checkedListBox1 中清除项目,但您将面临异常。

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

那是因为您已经添加了数据源!

改为使用:

checkedListBox1.DataSource = null;