我想获取asp.net中父文件夹中的文件夹(子文件夹)的数量。
我尝试了int directoryCount = Server.MapPath("~/folder1/folder2/").Length;
但没有得到正确的价值。
答案 0 :(得分:2)
使用DirectoryInfo.GetDirectories
方法获取目录并将其计为:
DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/folder1/folder2/"));
DirectoryInfo[] diArr = di.GetDirectories();
int count = dirArr.Length; //Total directories under the folder
上面将返回你路径下的子目录,它不是递归的,如果你想递归找到所有目录,那么使用重载DirectoryInfo.GetDirectories Method (String, SearchOption)
DirectoryInfo[] diArr = di.GetDirectories(Server.MapPath("~/folder1/folder2/"),
SearchOption.AllDirectories);