C#递归文件夹和文件目录
我正在尝试检索目录中所有文件夹中的所有文件。
但结果是相当随机的..
我认为 foreach 是错误的..
我不明白的是为什么?
因为在所有文件夹中,我们检查了所有文件,然后显示所有文件的链接按钮。但实际上它显示了很多文件夹,两次。
var DI = new DirectoryInfo("C://inetpub//wwwroot//ClientPortal//Files//")
.GetDirectories("*.*", System.IO.SearchOption.AllDirectories);
foreach (System.IO.DirectoryInfo D1 in DI)
{
System.IO.FileInfo[] fiArr = D1.GetFiles();
foreach (System.IO.FileInfo file in fiArr)
{
LinkButton lktest = new LinkButton();
lktest.Text = D1.Name;
form1.Controls.Add(lktest);
form1.Controls.Add(new LiteralControl("<br>"));
}
}
有人可以帮助我吗?
多谢 !
I am trying to retrieve all the files in all the folders I have in a directory .
But the result is quite random ..
I think the foreach is wrong ..
What I don't understand is why ?
Because in all the folders , we check all the files and then display a link buttons of all the files . But actually it's displaying a lot of folders , twice .
var DI = new DirectoryInfo("C://inetpub//wwwroot//ClientPortal//Files//")
.GetDirectories("*.*", System.IO.SearchOption.AllDirectories);
foreach (System.IO.DirectoryInfo D1 in DI)
{
System.IO.FileInfo[] fiArr = D1.GetFiles();
foreach (System.IO.FileInfo file in fiArr)
{
LinkButton lktest = new LinkButton();
lktest.Text = D1.Name;
form1.Controls.Add(lktest);
form1.Controls.Add(new LiteralControl("<br>"));
}
}
Can someone help me ?
Thanks a lot !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在这里,您正在创建名称设置为目录的链接按钮,而听起来您想要的是文件(即 file.Name 而不是 D1.Name)
Here you're creating link buttons with the name set to the directory when it sounds like you want the file instead (ie file.Name instead of D1.Name)
这有帮助吗?
http://www.dreamincode.net/code/snippet1669.htm
Does this help?
http://www.dreamincode.net/code/snippet1669.htm
第一行代码似乎是罪魁祸首:
尝试使用以下内容:
The first line of code seems like the culprit:
Try using the following: