首页 新闻 会员 周边 捐助

C#怎么读取某一文件夹下的所有文件夹和文件。

0
悬赏园豆:10 [已解决问题] 解决于 2009-09-23 11:40

例如: 我E盘下有个文件夹叫 demo

demo下有N个文件夹

demo1

demo2

demo3

。。。

demo1 下也有N个文件或者是文件夹

大概就是这个意思 。我的任务是将他全部显示出来

-demo1

--demo1_1

---demo1_1_1

---demo.jar

---demo1_1

-demo2

--demo2_1

-demo3

。。。。。。

 

也不知道表述清不清楚 !   相关资料也行 。 多谢了 、

问题补充: 好要得到所有文件(夹)的创建时间,以及大小 。 呵呵 、
Yellows的主页 Yellows | 初学一级 | 园豆:45
提问于:2009-09-22 17:52
< >
分享
最佳答案
1

用递归是肯定的!.具体实现见下代码

 

  private void button1_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("目录试图"));

递归方法(
"C:\\Downloads", this.treeView1.Nodes[0]);
}
private void 递归方法(string path, TreeNode node)
{
if (!Directory.Exists(path)) return;
//目录下的文件、文件夹集合
string[] diArr = System.IO.Directory.GetDirectories(path, "*", System.IO.SearchOption.AllDirectories);
string[] rootfileArr = System.IO.Directory.GetFiles(path);
//文件夹递归
for (int i = 0; i < diArr.Length; i++)
{
//增加父节点
string fileName = Path.GetFileNameWithoutExtension(diArr[i]);
node.Nodes.Add(fileName);

递归方法(diArr[i], node.Nodes[i]);
}

//文件直接添加
foreach (string var in rootfileArr)
{
node.Nodes.Add(Path.GetFileNameWithoutExtension(var));
}

}

 


效果如图

你可以在处理一下、文件夹加个图片,文件根据类型加个图片..就ok了

收获园豆:2
邢少 | 专家六级 |园豆:10926 | 2009-09-23 09:16
您好 , 您的答案最接近了 。 但是还有个问题 。 我要得到所有文件的创建时间,以及大小 不知道 您能否 。。。 继续教教我这个新人呢
Yellows | 园豆:45 (初学一级) | 2009-09-23 10:45
@小尾尾: 你想、文件名都得到了,还有什么不能得到的呢。这个代码中是得到了文件的路径。你把这个路径直接声明一个文件、或文件夹的对象 System.IO.FileInfo、 System.IO.DirectoryInfo 它们就包含了你说的这些个属性。文件
邢少 | 园豆:10926 (专家六级) | 2009-09-23 11:44
我很菜的 一点也搞不懂 55555555
Yellows | 园豆:45 (初学一级) | 2009-09-23 12:01
其他回答(3)
0

基本方法是使用递归。

请参考以下链接:

http://topic.csdn.net/t/20050706/09/4125613.html

收获园豆:2
Kinglee | 园豆:3158 (老鸟四级) | 2009-09-22 18:42
看了一下给的资料,没懂!
支持(0) 反对(0) Yellows | 园豆:45 (初学一级) | 2009-09-23 10:17
0

可以用递归
http://www.cnblogs.com/22de4/archive/2007/06/11/779512.html

也可以用API

http://www.cnblogs.com/xdesigner/archive/2006/12/08/586177.html

刚刚写了个简单的,你可以试下

  // Make sure the specified path exists
public static bool _VerifyPath(String path)
{
return System.IO.Directory.Exists(path);
}
public static void GetDirectory(string strDirName)
{
if (!_VerifyPath(strDirName)) { return; }
string[] diArr = System.IO.Directory.GetDirectories(strDirName, "*", System.IO.SearchOption.AllDirectories);
string[] rootfiArr = System.IO.Directory.GetFiles(strDirName);
{
if (rootfiArr != null)
{
foreach (string fi in rootfiArr) { Console.WriteLine(fi); }
}
}
foreach (string dri in diArr)
{
Console.WriteLine(dri);
System.IO.DirectoryInfo di
= new System.IO.DirectoryInfo(dri);
{
if (dri != null)
{
string[] fiArr = System.IO.Directory.GetFiles(dri);
if (fiArr != null)
{
foreach (string fi in fiArr) { Console.WriteLine(fi); }
}
}
}
}
}

 

测试 :

 GetDirectory(@"c:\test\");
Console.ReadKey();


思路清晰,但效率不高。

收获园豆:3
邀月 | 园豆:25475 (高人七级) | 2009-09-22 18:56
0

///刚做的小例子  要想实现下层文件夹的文件内容 则需要实现多次循环

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string str1;
        int j=0;
        private void toolStripButton1_Click(object sender, EventArgs e)//核心代码 
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                ListViewGroup listgourp1 = new ListViewGroup("文件", HorizontalAlignment.Left);
                ListViewGroup listgroup2 = new ListViewGroup("文件夹", HorizontalAlignment.Left);
                listView1.Groups.AddRange(new ListViewGroup[] { listgourp1, listgroup2 });
                toolStripTextBox1.Text = folderBrowserDialog1.SelectedPath;
                DirectoryInfo dr = new DirectoryInfo(toolStripTextBox1.Text);
                DirectoryInfo[] di = dr.GetDirectories();
                foreach(DirectoryInfo s  in di)
                   
                {
                 
                    listView1.Items.Add(s.Name);
                   // listView1.Items[listView1.Items.Count - 1].ImageIndex = 1;
                    listView1.Items[listView1.Items.Count - 1].SubItems.Add(s.FullName);
                    listView1.Items[listView1.Items.Count - 1].SubItems.Add("");
                    listView1.Items[listView1.Items.Count - 1].SubItems.Add(s.CreationTime.ToShortDateString());
                    listView1.Items[listView1.Items.Count - 1].Group = listgroup2;

                    foreach (FileInfo f in s.GetFiles())
                    {
                        j++;
                        str1 += s.Name + "+j+";
                 

                        listView1.Items.Add(f.Name);
                       // listView1.Items[listView1.Items.Count - 1].ImageIndex = 0;
                        listView1.Items[listView1.Items.Count - 1].Group = listgourp1;
                        listView1.Items[listView1.Items.Count - 1].SubItems.Add(f.FullName);
                        listView1.Items[listView1.Items.Count - 1].SubItems.Add(f.Length.ToString());
                        listView1.Items[listView1.Items.Count - 1].SubItems.Add(s.CreationTime.ToShortDateString());


                    }
                    foreach (DirectoryInfo s1 in s.GetDirectories())
                    {
                        listView1.Items.Add(s1.Name);
                       // listView1.Items[listView1.Items.Count - 1].ImageIndex = 1;
                        listView1.Items[listView1.Items.Count - 1].Group = listgroup2;
                        listView1.Items[listView1.Items.Count - 1].SubItems.Add(s1.FullName);
                        listView1.Items[listView1.Items.Count - 1].SubItems.Add("");
                        listView1.Items[listView1.Items.Count - 1].SubItems.Add(s1.CreationTime.ToShortDateString());
                     
                     foreach(FileInfo f in s1.GetFiles())
                     {
                         j++;
                        str1 += f.Name+"+j+" ;
               
                    listView1.Items.Add(f.Name);
                  //  listView1.Items[listView1.Items.Count - 1].ImageIndex = 0;
                    listView1.Items[listView1.Items.Count - 1].Group = listgourp1;
                    listView1.Items[listView1.Items.Count-1].SubItems.Add(f.FullName);
                    listView1.Items[listView1.Items.Count - 1].SubItems.Add(f.Length.ToString());
                    listView1.Items[listView1.Items.Count - 1].SubItems.Add(s.CreationTime.ToShortDateString());

                 
                  }
                 }
             
                }
            
            
            }
        }

        private void 大图表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.LargeIcon;
        }

        private void 小图表ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.SmallIcon;
        }

        private void 详细信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.Details;
        }

        private void 列标ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.List;
        }

        private void 标题ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            listView1.View = View.Tile;
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
    
        }

        private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 newfrm = new Form2();
            newfrm.Show();
        }
      
        
    }
}

收获园豆:3
.net 丁 | 园豆:208 (菜鸟二级) | 2009-09-22 21:57
http://www.cnblogs.com/dingxuedong/ 这个博客可以下载到资源
支持(0) 反对(0) .net 丁 | 园豆:208 (菜鸟二级) | 2009-09-22 22:05
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册