用递归是肯定的!.具体实现见下代码
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了
可以用递归
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();
思路清晰,但效率不高。
///刚做的小例子 要想实现下层文件夹的文件内容 则需要实现多次循环
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();
}
}
}