/* ######### ############ ############# ## ########### ### ###### ##### ### ####### #### ### ########## #### #### ########### #### #### ########### ##### ##### ### ######## ##### ##### ### ######## ###### ###### ### ########### ###### ###### #### ############## ###### ####### ##################### ###### ####### ###################### ###### ####### ###### ################# ###### ####### ###### ###### ######### ###### ####### ## ###### ###### ###### ####### ###### ##### ##### ###### ##### ##### #### ##### #### ##### ### ##### ### ### # ### ### ### ## ### ### __________#_______####_______####______________ 我们的未来没有BUG * ============================================================================== * Filename: Text * Created: 2016/4/26 11:45 * Author: HaYaShi WangYuChen * ============================================================================== */ using UnityEngine; using System.Collections; using System.IO; using System; public class Text : MonoBehaviour { /// <summary> /// 通过判断获取该父文件夹文件数量或所有子父文件夹文件数量 /// </summary> /// <param name="physicelPath">传入的是物理路径</param> /// <param name="isIncludeSubDirectory"></param> /// <returns></returns> public static int FileCount(string physicelPath, bool isIncludeSubDirectory) { int count = 0; if (isIncludeSubDirectory) { //该父文件夹文件数量以及所有子文件夹文件数量 count = Directory.GetFiles(physicelPath, "*.*", SearchOption.AllDirectories).Length; } else { //该父文件夹文件数量 count = Directory.GetFiles(physicelPath, "*.*", SearchOption.TopDirectoryOnly).Length; } return count; } /// <summary> /// 通过判断获取该父文件夹数量或所有子父文件夹数量 /// </summary> /// <param name="physicelPath">传的是布尔值的参数</param> /// <param name="isIncludeSubDirectory"></param> /// <returns></returns> public static int DirectoryCount(string physicelPath, bool isIncludeSubDirectory) { int count = 0; if (isIncludeSubDirectory) { //该父文件夹数量以及所有子文件夹数量 count = Directory.GetDirectories(physicelPath, "*.*", SearchOption.AllDirectories).Length; } else { //该父文件夹数量 count = Directory.GetDirectories(physicelPath, "*.*", SearchOption.TopDirectoryOnly).Length; } return count; } /// <summary> /// 获取某一目录之下所有文件的大小 /// </summary> /// <param name="physicelPath">文件夹路径</param> /// <returns></returns> public static long TotalFileSizes(string physicelPath) { string[] a = Directory.GetFiles(physicelPath, "*.*", SearchOption.AllDirectories); //Directory.GetFiles(@"D:Test", "*.jpg", SearchOption.AllDirectories); //找出该目录(及下级)所有JPG文件 long b = 0; foreach (var item in a) { FileInfo info = new FileInfo(item); b += info.Length; } return b; } /// <summary> /// 操作文件夹,删除所有空文件夹 /// </summary> /// <param name="physicalpath">物理路径</param> public static void RemoveAllEmptyDirectories(string physicalpath) { //遍历旗下子文件夹 foreach (var item in Directory.GetDirectories(physicalpath)) { //循环遍历旗下子文件夹 RemoveAllEmptyDirectories(item); //判断文件夹里面文件为空 同时 判断旗下子文件夹里面文件为空 if (Directory.GetFiles(item).Length == 0 && Directory.GetDirectories(item).Length == 0) { //删除文件 Directory.Delete(item, false); } } } /// <summary> /// 删除某一路径之下所有文件夹下文件 /// </summary> /// <param name="physicalPath">物理路径</param> public static void DeleteAllFiles(string physicalPath) { DirectoryInfo id = new DirectoryInfo(physicalPath); //遍历删除此文件夹下的文件 foreach (FileInfo item in id.GetFiles()) { item.Delete(); } //遍历子文件夹数组 foreach (DirectoryInfo item in id.GetDirectories()) { DeleteAllFiles(item.FullName); } } /// <summary> /// 删除某一路径之下 days天之内所有文件夹下文件 /// </summary> /// <param name="physicalPath">物理路径</param> /// <param name="days">天数</param> public static void DeleteAllFilesDays(string physicalPath, int days) { int day = -(Math.Abs(days)); DirectoryInfo id = new DirectoryInfo(physicalPath); //遍历删除此文件夹下的文件 foreach (FileInfo item in id.GetFiles()) { Debug.Log("文件创建时间"+item.CreationTime); Debug.Log("前" + days + "天现在时间" + DateTime.Now.AddDays(day)); //判断文件是否大于前days时间 if (item.CreationTime>DateTime.Now.AddDays(day)) { item.Delete(); } } //遍历子文件夹数组 foreach (DirectoryInfo item in id.GetDirectories()) { DeleteAllFilesDays(item.FullName,days); } } void Start () { //Debug.Log(TotalFileSizes("C:\Users\EasyAR\Desktop\Text")+":字节"); //Debug.Log("该父文件夹文件数量以及所有子文件夹文件数量" + FileCount("D:\hahaha", true)); //Debug.Log("该父文件夹文件数量" + FileCount("D:\hahaha", false)); //Debug.Log("该父文件夹数量以及所有子文件夹数量" + DirectoryCount("D:\hahaha", true)); //Debug.Log("该父文件夹数量" + DirectoryCount("D:\hahaha", false)); //Debug.Log(TotalFileSizes("C:\Users\EasyAR\Desktop\Text") + ":字节"); //RemoveAllEmptyDirectories("D:\hahaha\xixixi"); //DeleteAllFiles("D:\hahaha\xixixi"); //DeleteAllFilesDays("D:\hahaha\xixixi",10); } }
最近对获取文件夹研究一下,总结了一点,希望你们有用。