欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

asp.net 获取目录下的文件数和文件夹数

程序员文章站 2024-03-08 13:02:10
复制代码 代码如下: int j = 0; protected void button1_click(object sender, eventargs e) { direc...
复制代码 代码如下:

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 <directorys.length; i )
{
response.write(directorys " <br/>");
}
response.write("统计目录:" directorys.length "个 <br/>--------- <br>");
//=================================================
string[] files = system.io.directory.getfiles("d:\\","*");
for(int i=0; i <files.length; i )
{
response.write(files " <br/>");
}
response.write("统计文件:" files.length "个 <br/>--------- <br>");