积极答复者
循环文件夹,读取它的名称

问题
答案
-
你好,全部代码如下:
我在自己机器上测试过,工作的很好,程序是Winform的,但算法与WebForm相同using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.IO; using System.Windows.Forms; namespace SMS { public partial class frmTreeView : Form { public frmTreeView() { InitializeComponent(); } private void frmTreeView_Load(object sender, EventArgs e) { string path = @"D:\zwf\ebook"; TreeNode node = new TreeNode(path); twList.Nodes.Add(node); GetDirectory(node,path); } /// <summary> /// 遍历当前目录下的所有文件夹和文件 /// 搜索当前目录下的所有文件夹,添加到TreeView节点中递归遍历当前目录 /// </summary> /// <param name="node">父节点</param> /// <param name="path">目录路径</param> private void GetDirectory(TreeNode node,string path) { string[] Directorys = Directory.GetDirectories(path); foreach (string directory in Directorys) { string directoryName = DirectoryName(directory); TreeNode childTreeNode = new TreeNode(directoryName); node.Nodes.Add(childTreeNode); GetDirectory(childTreeNode,directory); } } /// <summary> /// 获取文件或者文件夹名称 /// 如:输入D:\zwf\ebook\ /// 输出ebook /// </summary> /// <param name="path"></param> /// <returns></returns> private string DirectoryName(string path) { ///最后出现字符"\"的索引 int index = path.LastIndexOf(@"\"); ///路径字符长度 int length=path.Length; ///返回文件或者文件夹名称 return path.Substring(index+1, length - index-1); } /// <summary> /// 选择节点后获取当前节点下的所有文件名称 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void twList_AfterSelect(object sender, TreeViewEventArgs e) { lwFileList.Items.Clear(); ///获取当前选中节点的完整路径 string filePath = twList.SelectedNode.FullPath.ToString(); GetFiles(filePath); } /// <summary> /// 获取当前路径下的所有文件 /// </summary> /// <param name="filePath"></param> private void GetFiles(string filePath) { string[] files = Directory.GetFiles(filePath); ///遍历文件并添加至listview中 foreach (string file in files) { string fileName = DirectoryName(file); lwFileList.Items.Add(fileName); } } } }
谢谢!
http://blodfox777.cnblogs.com- 已标记为答案 今天有小雨 2009年4月23日 9:26
全部回复
-
楼主你好!
可以采用递归算法为TreeView动态添加节点,可以参考如下代码:
来自:http://www.cnblogs.com/zwffff/archive/2008/11/10/1330871.html/// <summary> /// 遍历当前目录下的所有文件夹和文件 /// 搜索当前目录下的所有文件夹,添加到TreeView节点中递归遍历当前目录 /// /// </summary> /// <param name="node">父节点</param> /// <param name="path">目录路径</param> private void GetDirectory(TreeNode node, string path) { string[] Directorys = Directory.GetDirectories(path); foreach (string directory in Directorys) { string directoryName = DirectoryName(directory); TreeNode childTreeNode = new TreeNode(directoryName); node.Nodes.Add(childTreeNode); GetDirectory(childTreeNode, directory); } }
谢谢!
http://blodfox777.cnblogs.com -
你好,如图所示,上面的代码正是实现了此功能:
你可以参考:http://www.cnblogs.com/zwffff/archive/2008/11/10/1330871.html
获得更多信息和源码。
谢谢!
http://blodfox777.cnblogs.com -
你好,你上面的代码我看过了,但是不知道那个 DirectoryName 是怎么来的.
所以我修改了一下之后就把所有目录名作为同一级别的节点显示的.
请问如何解决呢?
private void GetDirectory(string path)
{
string[] Directorys = Directory.GetDirectories(path);
foreach (string directory in Directorys)
{
TreeNode pronode=new TreeNode(directory.ToString());
this.TreeView1.Nodes.Add(pronode);
GetDirectory(directory);
}
} -
你好,全部代码如下:
我在自己机器上测试过,工作的很好,程序是Winform的,但算法与WebForm相同using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.IO; using System.Windows.Forms; namespace SMS { public partial class frmTreeView : Form { public frmTreeView() { InitializeComponent(); } private void frmTreeView_Load(object sender, EventArgs e) { string path = @"D:\zwf\ebook"; TreeNode node = new TreeNode(path); twList.Nodes.Add(node); GetDirectory(node,path); } /// <summary> /// 遍历当前目录下的所有文件夹和文件 /// 搜索当前目录下的所有文件夹,添加到TreeView节点中递归遍历当前目录 /// </summary> /// <param name="node">父节点</param> /// <param name="path">目录路径</param> private void GetDirectory(TreeNode node,string path) { string[] Directorys = Directory.GetDirectories(path); foreach (string directory in Directorys) { string directoryName = DirectoryName(directory); TreeNode childTreeNode = new TreeNode(directoryName); node.Nodes.Add(childTreeNode); GetDirectory(childTreeNode,directory); } } /// <summary> /// 获取文件或者文件夹名称 /// 如:输入D:\zwf\ebook\ /// 输出ebook /// </summary> /// <param name="path"></param> /// <returns></returns> private string DirectoryName(string path) { ///最后出现字符"\"的索引 int index = path.LastIndexOf(@"\"); ///路径字符长度 int length=path.Length; ///返回文件或者文件夹名称 return path.Substring(index+1, length - index-1); } /// <summary> /// 选择节点后获取当前节点下的所有文件名称 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void twList_AfterSelect(object sender, TreeViewEventArgs e) { lwFileList.Items.Clear(); ///获取当前选中节点的完整路径 string filePath = twList.SelectedNode.FullPath.ToString(); GetFiles(filePath); } /// <summary> /// 获取当前路径下的所有文件 /// </summary> /// <param name="filePath"></param> private void GetFiles(string filePath) { string[] files = Directory.GetFiles(filePath); ///遍历文件并添加至listview中 foreach (string file in files) { string fileName = DirectoryName(file); lwFileList.Items.Add(fileName); } } } }
谢谢!
http://blodfox777.cnblogs.com- 已标记为答案 今天有小雨 2009年4月23日 9:26
-
你好,其实 Lance ZHang 意思是对的,你试试下面的代码, 你要保证Aspnet 用户在你要访问的目录的相当的权限
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.BuildDirectoryTree(this.TreeView1.Nodes, @"D:\A"); } } private void BuildDirectoryTree(TreeNodeCollection fNodes, string fPath) { foreach (string filePath in Directory.GetDirectories(fPath)) { TreeNode fNode = new TreeNode(); fNode.Text = Path.GetDirectoryName(filePath); fNodes.Add(fNode); this.BuildDirectoryTree(fNode.ChildNodes, filePath); } }
知识改变命运,奋斗成就人生!