asp.net遍历目录文件夹和子目录所有文件
asp.net遍历目录文件夹和子目录所有文件
发布时间:2016-12-29 来源:查字典编辑
摘要:复制代码代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.Text;...

复制代码 代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.IO;

usingSystem.Threading;

namespacecopefile

{

classProgram

{

staticvoidMain(string[]args)

{

stringtestDir="e:/xunlei/";

listFiles(testDir,0);

Console.ReadKey();

}

publicstaticvoidlistFiles(stringdir,intlevel)

{

//阿会楠练习作品,程序多有参考

try

{

//获取文件列表

string[]files=Directory.GetFiles(dir);

StringpreStr="";

for(inti=0;i<level;i++)

{

preStr+="";

}

foreach(stringfinfiles)

{

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(stringdindirs)

{

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(Exceptionex)

{

Console.WriteLine(ex.Message);

}

}

}

}

推荐文章
猜你喜欢
附近的人在看
推荐阅读
拓展阅读
相关阅读
网友关注
最新asp.net教程学习
热门asp.net教程学习
编程开发子分类