C#遍历目录下的所有子目录和文件
时间:2010-09-16 来源:hlhcto
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace Cms.Inc
- {
- public class FilesList
- {
- public FilesList()
- {
- }
- public string DirList()
- {
- StringBuilder html=new StringBuilder();
- DirectoryInfo dir = new DirectoryInfo(@"E:\webapp\web\Cms");
- html.Append("目录列表:<br />");
- foreach(DirectoryInfo cdir in dir.GetDirectories("*"))
- {
- html.Append(cdir.Name + "<br />");
- }
- return html.ToString();
- }
- public string FileList()
- {
- StringBuilder html = new StringBuilder();
- DirectoryInfo dir = new DirectoryInfo(@"E:\webapp\web\Cms\template\default");
- html.Append("文件列表:<br />");
- foreach (FileInfo f in dir.GetFiles("show_*.html"))//显示以'show_'开头的所有html文件
- {
- html.Append(f.Name+"<br />");
- }
- return html.ToString();
- }
- }
- }
显示:
Cms.Inc.FilesList f = new BoyanCms.Inc.FilesList();
Response.Write(f.DirList());
Response.Write(f.FileList());
相关阅读 更多 +