三种获取指定文件夹下所有的文件名称方法
时间:2011-01-19 来源:秩名
返回包含文件名的路径.然后再截取文件名
如:
string[] filenames=directory.getfiles(path);for(string files in filenames)
{
response.write(files.replace(path,""));
}
方法二:
directoryinfo dir = new directoryinfo(@"c:data");
fileinfo[] finfo = dir.getfiles();
string fnames = string.empty;
for (int i = 0; i < finfo.length; i++)
{
fnames += finfo[i].name + " <br> ";
}
response.write(fnames);
方法三
/// <summary>
/// 获取指定文件夹下所有的文件名称
/// </summary>
/// <param name="foldername">指定文件夹名称,绝对路径</param>
/// <param name="filefilter">文件类型过滤,根据文件后缀名,如:*,*.txt,*.xls</param>
/// <param name="iscontainsubfolder">是否包含子文件夹</param>
/// <returns>arraylist数组,为所有需要的文件路径名称</returns>
public static arraylist getallfilesbyfolder(string foldername, string filefilter,bool iscontainsubfolder)
{
arraylist resarray = new arraylist ();
string[] files = directory.getfiles(foldername, filefilter);
for (int i = 0; i < files.length ; i++)
{
resarray.add(files[i]);
}
if (iscontainsubfolder)
{
string[] folders = directory.getdirectories(foldername);
for (int j = 0; j < folders.length; j++)
{
//遍历所有文件夹
arraylist temp = getallfilesbyfolder(folders[j], filefilter, iscontainsubfolder);
resarray.addrange(temp);
}
}
return resarray;
}
/// <summary>
/// 获取指定文件夹下所有的文件名称,不过滤文件类型
/// </summary>
/// <param name="foldername">指定文件夹名称,绝对路径</param>
/// <param name="iscontainsubfolder">是否包含子文件夹</param>
/// <returns>arraylist数组,为所有需要的文件路径名称</returns>
public static arraylist getallfilesbyfolder(string foldername, bool iscontainsubfolder)
{
return getallfilesbyfolder(foldername, "*", iscontainsubfolder);
}
标签分类: