1

我有一个checkedListBox;将文件加载到文件夹中;检查时运行/打开。

我想要实现的是:
- 将不带扩展名的文件名加载到 CheckedListBox 中。

我可以检索:

"C:\Folder1\anotherfolder\myfile1.txt"

但; 我只想被检索:“文件名”(有或没有扩展名)。

就像是:

"myfile1.txt"

我试图用 folderBrowserDialog 做到这一点,但我不知道如何做到这一点。

我当前的代码:

//...
    private string openFileName, folderName;
    private bool fileOpened = false;
//...

        OpenFileDialog ofd = new OpenFileDialog();
        FolderBrowserDialog fbd = new FolderBrowserDialog();

        if (!fileOpened)
        {
            ofd.InitialDirectory = fbd.SelectedPath;
            ofd.FileName = null;


            fbd.Description = "Please select your *.txt folder";
            fbd.RootFolder = System.Environment.SpecialFolder.MyComputer;
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string foldername = fbd.SelectedPath;
                foreach (string f in Directory.GetFiles(foldername))
                checkedListBox1.Items.Add(f);
            }

谁能指出我正确的方向?提前致谢。

4

1 回答 1

1

您根本不需要 OpenFileDialog,只需更改将文件添加到的行

checkedListBox1.Items.Add(Path.GetFileName(f));

只记得添加

using System.IO;

您还可以将所有内容简化为一行代码

checkedListBox1.Items.AddRange(Directory.GetFiles(fbd.SelectedPath).Select(x => Path.GetFileName(x)).ToArray());
于 2013-05-04T11:39:54.933 回答