当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net遍历目录文件夹和子目录所有文件

ASP.NET
blog程序新版本V2.0 Beta完成,提供V1.0全部源码下载
“/”应用程序中的服务器错误
asp.net(c#)复数类(复数加减乘除四则运算)
asp.net(c#)不可访问,因为它受保护级别限制
asp.net(c#) 水仙花数
ASP.NET实现用图片进度条显示投票结果
asp.net(c#) MS AJAX的安装
asp.net(c#)有关 Session 操作的几个误区
ASP.net基础知识之常见错误分析
[.net] 操纵自如-页面内的配合与通信
c# static的全部用法收集整理
使CheckBoxList的Attributes属性生效(修改微软的一个bug)
在ASP.NET中使用Session常见问题集锦
[原创]完美解决Could not load file or assembly ''AjaxPro.2'' or one of its dependencies. 拒绝访问。
在Apache环境下成功的运行ASP.NET的注意事项
ClickOnce DIY全自动更新下载升级的自我实现
asp.net jscript 一句话木马
在ASP.Net中实现flv视频转换的代码
将DataTable中的一行复制到另一个DataTable的方法
在DataTable中执行Select("条件")后,返回DataTable的方法

ASP.NET 中的 asp.net遍历目录文件夹和子目录所有文件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 42 ::
收藏到网摘: n/a

用asp.net实现遍历目录文件和子目录的代码
复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
namespace copefile
{
class Program
{
static void Main(string[] args)
{
string testDir = "e:/xunlei/";
listFiles(testDir,0);
Console.ReadKey();
}
public static void listFiles(string dir, int level)
{
//阿会楠练习作品,程序多有参考
try
{
//获取文件列表
string[] files = Directory.GetFiles(dir);
String preStr = "";
for (int i = 0; i < level; i++)
{
preStr += " ";
}
foreach (string f in files)
{
if (f.LastIndexOf("\\") == -1)
{
Console.WriteLine(preStr + f.Substring(f.LastIndexOf("/") + 1));
}
else
{
Console.WriteLine(preStr + f.Substring(f.LastIndexOf("\\") + 1));
}
}
//获取目录列表
string[] dirs = Directory.GetDirectories(dir);
foreach (string d in dirs)
{
if (d.LastIndexOf("\\") == -1)
{
Console.WriteLine(preStr + d.Substring(d.LastIndexOf("/") + 1));
}
else
{
Console.WriteLine(preStr + d.Substring(d.LastIndexOf("\\") + 1));
}
if (Directory.Exists(d))
{
listFiles(d, level + 1);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}