如何从枚举文件夹向列表框中添加不同的文件 [英] How Can I Add Differnt Files In To Listbox From Enumeratedfolders

查看:102
本文介绍了如何从枚举文件夹向列表框中添加不同的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好..朋友,我需要一些帮助..关于文件夹..
我使用了folderbrowsedilog并选择了一个文件夹,该文件夹包含更多文件夹..
现在我的任务是..将文件从第一个文件夹移至listbox1,将第二个文件夹移至listbox2 ..依此类推..

例如..
第一个文件夹包含10个文件,我希望将10个文件添加到listbox1;
第二个文件夹包含6个文件,我希望将6个文件添加到listbox2;
..
.
.

有可能..


这是我的代码...尝试对其进行一些修改..

hello.. friends i need some help.. regarding the folders..
i have used a folderbrowsedilog and selected a folder and that folder contains some more folders..
now my task is .. to get files from first folder to listbox1 and second folder to listbox2.. and so on..

for example..
the first folder contains 10 files, and i want 10 files to be added to listbox1;
the second folder contains 6 files , i want 6 files to be added to listbox2;
..
.
.

is it possible..


here is my code ... try to make some modifications in it..

var fbd = new FolderBrowserDialog();
            fbd.ShowNewFolderButton = false;

            if (fbd.ShowDialog() == DialogResult.Cancel)
                return;

            string foldername = fbd.SelectedPath;

            var dirs = from dir in Directory.EnumerateDirectories(foldername )
                       select dir;

            foreach (var dir in dirs)
            {
                foreach (string f in Directory.EnumerateFiles(dir, "*.*"))
                  listBox1.Items.Add(f);

            }

推荐答案

您好,我尝试使用以下代码创建一个小的POC(概念证明):

考虑以下文件夹结构:

FolderA
| _ A1.txt
| _ A2.txt
| _ FolderB
| _ B1.txt
| _ B2.txt
| _ B3.txt

Hi, I tried to create a small POC (Proof of concept) using the following code:

Considering the following folder structure:

FolderA
|_ A1.txt
|_ A2.txt
|_ FolderB
|_ B1.txt
|_ B2.txt
|_ B3.txt

string[] files = Directory.GetFiles(@"D:\FolderA");
ListBox1.DataSource = files;
ListBox1.DataBind();

string[] folders = Directory.GetDirectories(@"D:\FolderA");
files = Directory.GetFiles(folders[0]);
ListBox2.DataSource = files;
ListBox2.DataBind();



输出如下:

ListBox1:
A1.txt
A2.txt

ListBox2:
B1.txt
B2.txt
B3.txt



Output would be as follows:

ListBox1:
A1.txt
A2.txt

ListBox2:
B1.txt
B2.txt
B3.txt


在这里,我得到了代码,它可以完美运行..很大程度上要感谢Debabrata_DAs的想法..



here i go i got the code and it works perfectly.. mostly thanks to Debabrata_DAs as he gave me idea..



var fbd = new FolderBrowserDialog();
fbd.ShowNewFolderButton = false;

if (fbd.ShowDialog() == DialogResult.Cancel)
return;

string foldername = fbd.SelectedPath;


var dirs = from dir in
Directory.EnumerateDirectories(@" "+foldername )
select dir;
int directoryCount;

foreach (var dir in dirs)
{
foreach (string f in Directory.EnumerateFiles(dir, "*.*"))
{
listBox1.Items.Add(f);


}
directoryCount = System.IO.Directory.GetFiles(@"" + dir).Length;
var lb = new ListBox();
lb.Dock = DockStyle.Bottom;
foreach (string f in Directory.EnumerateFiles (dir,"*.*"))
lb.Items.Add(f);
panel2.Controls.Add(lb);
string m = directoryCount.ToString();
MessageBox.Show(m);
}


这篇关于如何从枚举文件夹向列表框中添加不同的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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