从文件夹中获取文件到列表,将子目录添加到下拉列表中,并在列表中列出文件名 [英] Get files from folders to list, add subdirectories to dropdown and list filenames on list

查看:300
本文介绍了从文件夹中获取文件到列表,将子目录添加到下拉列表中,并在列表中列出文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先查看此屏幕截图,看看会发生什么:

Have a look at this screenshot first to see what happens:

您可以在此屏幕截图上看到的是我已经完成的操作.但是我想再添加一些内容.

What you can see on this screenshot is what I have done already.. But I want to add a few things more.

现在,通过单击添加到列表"按钮,所有具有完整路径的文件都存储在列表A中.它们的文件名存储在列表B中.以下是代码:

Right now by clicking at the "Add to list" button all files with their full path are stored in List A. Their file names are stored in List B. And heres the code for it:

 if (type == "folder")
                {
                    string listPath = this.configsPath.Text;
                    string[] filesToList = System.IO.Directory.GetFiles(listPath, "*.*", System.IO.SearchOption.AllDirectories);
                    foreach (string file in filesToList)
                    {
                        if (!configsChkList.Items.Contains(file))
                        {
                            configsChkList.Items.Add(file, false);
                            configsDestList.Items.Add(Path.GetFileName(file));
                        }
                    }
                }

我希望列表b也存储它们相对于在输入字段中指定的路径的路径.作为示例,列表A中的最后三个条目位于名为"undermappe"的子目录中,但是由于我使用Path. GetFileName不能将条目存储在列表B的子目录中..如何做到这一点?

I want List b to also store their path relative to the path specified in the input field.. The last three entries in List A as an example are in a sub-directory called "undermappe", but since I use Path.GetFileName to store the entries in List B the sub-directory does not get viewed.. How can I do this?

然后是另一件事.子目录也应存储在顶部的ComboBox中,但只能存储目录,不能存储名称!我该怎么办?

And then another thing. The sub-directories should also be stored in the ComboBox on top, but only the directories not the names! How can I do this ?

推荐答案

             **Hi.
             try this example , i think this is what you need , 100% working:**


             System.String basefolder = "NASSIM\\LOUCHANI\\";

             foreach (String file in Directory.GetFiles(basefolder,"*.*"))
             {
                 comboBox1.Items.Add(Path.GetFileNameWithoutExtension(file));
                 //Load file name without extension / path.
             }

这篇关于从文件夹中获取文件到列表,将子目录添加到下拉列表中,并在列表中列出文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆