asp.net遍歷目錄文件夾和子目錄所有文件
更新時間:2008年05月01日 09:40:17 作者:
用asp.net實現(xiàn)遍歷目錄文件和子目錄的代碼
復(fù)制代碼 代碼如下:
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)
{
//阿會楠練習(xí)作品,程序多有參考
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);
}
}
}
}
相關(guān)文章
.Net中導(dǎo)出數(shù)據(jù)到Excel(asp.net和winform程序中)
.Net中導(dǎo)出數(shù)據(jù)到Excel包括以下兩種情況:asp.net中導(dǎo)出Excel的方法/winForm中導(dǎo)出Excel的方法,針對以上兩種情況做了下詳細的實現(xiàn)代碼,感興趣的朋友可不要錯過了哈,希望本文對你有所幫助2013-02-02
.net實現(xiàn)微信公眾賬號接口開發(fā)實例代碼
這篇文章主要介紹了.net實現(xiàn)微信公眾賬號接口開發(fā)實例代碼,有需要的朋友可以參考一下2013-12-12
asp.net mvc webapi 實用的接口加密方法示例
本篇文章主要介紹了asp.net mvc webapi 實用的接口加密方法示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
ASP.NET2.0使用Enter Key作為默認提交問題分析(附源碼)
這篇文章主要介紹了ASP.NET2.0使用Enter Key作為默認提交,結(jié)合實例形式分析了ASP.NET2.0使用Enter Key默認提交的注意事項與相關(guān)實現(xiàn)技巧,并附上源碼供讀者參考,具有一定參考借鑒價值,需要的朋友可以參考下2015-11-11

