int j = 0;
protected void button1_click(object sender, eventargs e)
{
directoryinfo dir = new directoryinfo(textbox1.text.tostring());
label1.text = getallfiles(dir).tostring();
}getallfiles方法为自定义方法,实现遍历整个文件夹文件的方法。代码如下:
public int getallfiles(directoryinfo dir)
{
filesysteminfo[] fileinfo = dir.getfilesysteminfos();
foreach (filesysteminfo i in fileinfo)
{
if (i is directoryinfo)
{
getallfiles((directoryinfo)i);
}
else
{
j++;
}
}
return j;
}
//=================================================
string[] directorys = system.io.directory.getdirectories("d:","*");
for(int i=0; i{
response.write(directorys "
");
}
response.write("统计目录:" directorys.length "个
---------
");
//=================================================
string[] files = system.io.directory.getfiles("d:","*");
for(int i=0; i{
response.write(files "
");
}
response.write("统计文件:" files.length "个
---------
");
asp教程.net:获取某目录下的所有文件(包括子目录下文件)的数量
asp.net教程 c# 获取指定文件夹下所有子目录及文件
using system;
using system.collections;
using system.collections.generic;
using system.text;
using system.web;
using system.io;
using system.data;
namespace ec
{
///
/// 获取指定文件夹下所有子目录及文件
///
public class getfoldlist
{
#region 获取指定文件夹下所有子目录及文件(树形)
/****************************************
* 函数名称:getfoldall(string path)
* 功能说明:获取指定文件夹下所有子目录及文件(树形)
* 参 数:path:详细路径
* 调用示列:
* string strdirlist = server.mappath("templates");
* this.literal1.text = ec.getfoldlist.getfoldall(strdirlist);
*****************************************/
///
/// 获取指定文件夹下所有子目录及文件
///
/// 详细路径
public static string getfoldall(string path)
{string str = "";
directoryinfo thisone = new directoryinfo(path);
str = listtreeshow(thisone, 0, str);
return str;}
///
/// 获取指定文件夹下所有子目录及文件函数
///
/// 指定目录
/// 默认起始值,调用时,一般为0
/// 用于迭加的传入值,一般为空
///
public static string listtreeshow(directoryinfo thedir, int nlevel, string rn)//递归目录 文件
{
directoryinfo[] subdirectories = thedir.getdirectories();//获得目录
foreach (directoryinfo dirinfo in subdirectories)
{if (nlevel == 0)
{
rn += "├";
}
else
{
string _s = "";
for (int i = 1; i <= nlevel; i++)
{
_s += "│ ";
}
rn += _s + "├";
}
rn += "" + dirinfo.name.tostring() + "
";
fileinfo[] fileinfo = dirinfo.getfiles(); //目录下的文件
foreach (fileinfo finfo in fileinfo)
{
if (nlevel == 0)
{
rn += "│ ├";
}
else
{
string _f = "";
for (int i = 1; i <= nlevel; i++)
{
_f += "│ ";
}
rn += _f + "│ ├";
}
rn += finfo.name.tostring() + "
";
}
rn = listtreeshow(dirinfo, nlevel + 1, rn);
}
return rn;
}
/****************************************
* 函数名称:getfoldall(string path)
* 功能说明:获取指定文件夹下所有子目录及文件(下拉框形)
* 参 数:path:详细路径
* 调用示列:
* string strdirlist = server.mappath("templates");
* this.literal2.text = ec.fileobj.getfoldall(strdirlist,"tpl","");
*****************************************/
///
/// 获取指定文件夹下所有子目录及文件(下拉框形)
///
/// 详细路径
///下拉列表名称
///默认选择模板名称
public static string getfoldall(string path, string dropname, string tplpath)
{
string strdrop = "";}
///
/// 获取指定文件夹下所有子目录及文件函数
///
/// 指定目录
/// 默认起始值,调用时,一般为0
/// 用于迭加的传入值,一般为空
/// 默认选择模板名称
///
public static string listtreeshow(directoryinfo thedir, int nlevel, string rn, string tplpath)//递归目录 文件
{
directoryinfo[] subdirectories = thedir.getdirectories();//获得目录foreach (directoryinfo dirinfo in subdirectories)
{rn += " if (tplpath.tolower() == dirinfo.name.tostring().tolower())
{
rn += " selected ";
}
rn += ">";if (nlevel == 0)
{
rn += "┣";
}
else
{
string _s = "";
for (int i = 1; i <= nlevel; i++)
{
_s += "│ ";
}
rn += _s + "┣";
}
rn += "" + dirinfo.name.tostring() + "";
fileinfo[] fileinfo = dirinfo.getfiles(); //目录下的文件
foreach (fileinfo finfo in fileinfo)
{
if (getfileextends(finfo.name.tostring()) == "htm" || getfileextends(finfo.name.tostring()) == "html" || getfileextends(finfo.name.tostring()) == "aspx")
{rn += " if (tplpath.tolower() == dirinfo.name.tostring().tolower()+"/"+finfo.name.tostring().tolower())
{
rn += " selected ";
}
rn += ">";if (nlevel == 0)
{
rn += "│ ├";
}
else
{
string _f = "";
for (int i = 1; i <= nlevel; i++)
{
_f += "│ ";
}
rn += _f + "│ ├";
}
rn += finfo.name.tostring() + "";
}
}
rn = listtreeshow(dirinfo, nlevel + 1, rn, tplpath);
}
return rn;
}
#endregion
#region 获取指定文件夹下目录
///
/// 获取指定文件夹下目录
///
/// 文件夹路径
///
public static datatable getfold(string path)
{
datatable dt = new datatable();
datacolumn dc;
datarow dr;dc = new datacolumn();
dc.datatype = system.type.gettype("system.string");
dc.columnname = "目录名";
dt.columns.add(dc);dc = new datacolumn();
dc.datatype = system.type.gettype("system.datetime");
dc.columnname = "创建时间";
dt.columns.add(dc);dc = new datacolumn();
dc.datatype = system.type.gettype("system.datetime");
dc.columnname = "修改时间";
dt.columns.add(dc);
if (!system.io.directory.exists(path))
{
throw new exception("不存在相应目录");
}
else
{
directoryinfo thisone = new directoryinfo(path);
directoryinfo[] subdirectories = thisone.getdirectories();//获得目录foreach (directoryinfo dirinfo in subdirectories)
{
dr = dt.newrow();
dr["目录名"] = dirinfo.name.tostring();
dr["创建时间"] = dirinfo.creationtime;
dr["修改时间"] = dirinfo.lastwritetime;dt.rows.add(dr);
}
}
return dt;
}
#endregion#region 获取指定目录下的文件
///
///
///
///
///
///
public static datatable getfils(string path)
{
datatable dt = new datatable();
datacolumn dc;
datarow dr;dc = new datacolumn();
dc.datatype = system.type.gettype("system.string");
dc.columnname = "文件名";
dt.columns.add(dc);dc = new datacolumn();
dc.datatype = system.type.gettype("system.string");
dc.columnname = "文件格式";
dt.columns.add(dc);dc = new datacolumn();
dc.datatype = system.type.gettype("system.datetime");
dc.columnname = "创建时间";
dt.columns.add(dc);
dc = new datacolumn();
dc.datatype = system.type.gettype("system.datetime");
dc.columnname = "修改时间";
dt.columns.add(dc);dc = new datacolumn();
dc.datatype = system.type.gettype("system.string");
dc.columnname = "所属目录";
dt.columns.add(dc);
dc = new datacolumn();
dc.datatype = system.type.gettype("system.string");
dc.columnname = "文件大小";
dt.columns.add(dc);if (!system.io.directory.exists(path))
{
throw new exception("不存在相应目录");
}
else
{
directoryinfo thisone = new directoryinfo(path);
// directoryinfo[] subdirectories = thisone.getdirectories();//获得目录
// foreach (directoryinfo dirinfo in subdirectories)
// {
fileinfo[] fileinfo = thisone.getfiles();
foreach (fileinfo finfo in fileinfo)
{
dr = dt.newrow();
dr["文件名"] = finfo.name;
dr["文件格式"] = finfo.extension;
dr["创建时间"] = finfo.creationtime;
dr["修改时间"] = finfo.lastwritetime;
dr["所属目录"] = finfo.directoryname;
dr["文件大小"] = finfo.length.tostring();
dt.rows.add(dr);
}
//}}
return dt;
}
#endregion
#region 取得文件后缀
/**********************************
* 函数名称:getfileextends
* 功能说明:取得文件后缀
* 参 数:filename:文件名称
* 调用示例:
* getremoteobj o = new getremoteobj();
* string url = @"http://www.baidu.com/img/logo.gif";
* string s = o.getfileextends(url);
* response.write(s);
* o.dispose();
* ********************************/
///
/// 取得文件后缀
///
/// 文件名称
///
public static string getfileextends(string filename)
{
string ext = null;
if (filename.indexof(.) > 0)
{
string[] fs = filename.split(.);
ext = fs[fs.length - 1];
}
return ext;
}
#endregion
}
}