创建以ListBox的项目命名的多个文件夹

问题描述 投票:0回答:1

点击Get List按钮:

  1. 我使用选定的文件名填充ListBox。
  2. 然后我分隔文件扩展名并填充另一个ListBox,删除点。

点击Create Folders按钮,

我想删除重复的扩展并创建以ListBox中的项目命名的文件夹。 即,创建名称为:docdocxdwg等的文件夹。

private void btn_list_Click(object sender, EventArgs e)
{
    listBox_ex.Items.Clear();
    FolderBrowserDialog FBD = new FolderBrowserDialog();
    if (FBD.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        lbl_path.Text = FBD.SelectedPath;
        listBox_name.Items.Clear();
        string[] filename = Directory.GetFiles(FBD.SelectedPath);

        foreach (string file in filename)
        {
            listBox_name.Items.Add(Path.GetFileName(file));
            listBox_ex.Items.Add(Path.GetExtension(file).Replace(".", ""));                  
        }
    }
}

private void btn_CreateFolder_Click(object sender, EventArgs e)
{
    FolderBrowserDialog FBD2 = new FolderBrowserDialog();
    if (FBD2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        lbl_pathCreated.Text = FBD2.SelectedPath;
    }

    string path = lbl_pathCreated.Text;
    if (!Directory.Exists(path)) {
        Directory.CreateDirectory(path);
    } else {
        MessageBox.Show("already exit");
    }
}

ListBox filled with File names and file extensions

c# winforms listbox
1个回答
1
投票

替代方法,使用2个List<string>对象存储当前文件的选择,文件扩展名为DistinctOrdered元素。

列表然后用作qazxsw poi。 更改DataSource时会重置qazxsw poi集合。

选择目标路径后,扩展列表中的每个项目都用于在所选路径中创建目录。 无需检查目录是否已存在:ListBox.DataSource只是忽略它。

您可能希望为用户删除的路径添加验证过程。用户可能选择了一个奇怪的目的地。

ListBox.Items
© www.soinside.com 2019 - 2024. All rights reserved.