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

