1. 程式人生 > >c# 刪除一個資料夾下的所有空資料夾

c# 刪除一個資料夾下的所有空資料夾

/// <summary>
        /// 刪除掉空資料夾
        /// 所有沒有子“檔案系統”的都將被刪除
        /// </summary>
        /// <param name="storagepath"></param>
        public static void KillEmptyDirectory(String storagepath)
        {
            DirectoryInfo dir = new DirectoryInfo(storagepath);
            DirectoryInfo[] subdirs = dir.GetDirectories("*.*", SearchOption.AllDirectories);
            foreach (DirectoryInfo subdir in subdirs)
            {
                FileSystemInfo[] subFiles = subdir.GetFileSystemInfos();
                if (subFiles.Count() == 0)
                {
                    subdir.Delete();
                }
            }
        }